MobileUI LogoMobileUI
/

🔌 ORMLite

Use object relational mapping for SQLite on Android and iOS


The ORMLite Plugin PURPLE brings object relational mapping on the basis of ORMLite to your app. This Plugin brings special ORMLite bindings for the SQLite database on iOS to make the cross-platform magic happen. Additionally, you get a unified way of configuring ORMLite and the underlying database. You don't have to write platform-specific code.

Plugin Infos
Dependency"io.nevernull:mobileui-plugin-ormlite"
Api Docsio.nevernull.mobileui.ormlite
Main Interfaceio.nevernull.mobileui.ormlite.DaoFactory

Configuring ORMLite and the database

To configure ORMLite and the underlying SQLite database for your project, you need to create an OrmLiteConfiguration bean. To accomplish this, you can create a Bean Factory by annotating a class with the @Factory annotation. Then, add a method annotated with @Singleton that returns the OrmLiteConfiguration as shown below:

Config.java
import io.micronaut.context.annotation.Factory;
import io.nevernull.mobileui.ormlite.OrmLiteConfiguration;
import javax.inject.Singleton;

@Factory
public class Config {

    @Singleton
    public OrmLiteConfiguration ormLiteConfiguration() {
        return OrmLiteConfiguration
                // The data classes that are annotated with ORMLite annotations (entities)
                .entities(Person.class, Team.class, Project.class)
                // The name of the SQLite file within you app's private database dir
                .name("project.db")
                // Migration version differentiating database versions
                .version(1)
                // Let the ORMLite Plugin manage table creation based on the entities (true)
                .autoCreateTables(true)
                .onCreate(s -> {
                    // Optionally: Run script to create the database schema
                    System.out.println("Created");
                })
                .onUpgrade((connection, oldVersion, newVersion) -> {
                    // Optionally: Run script to migrate
                    System.out.println(oldVersion);
                    System.out.println(newVersion);
                });
    }
}

Note: To learn more about entities and ORMLite's annotation to map object into the database, please refer to the ORMLite documentation.

Using ORMLite to manage data

As soon as the configuration is available, you can simply inject the DaoFactory into your code. You can obtain a Dao (Data Access Object) with the getDao(<class>) method as shown below. Make sure, you are running this code after injection has taken place. This can be done within a method annotated with the javax.annotation.PostConstruct annotation.

PersonController.java
@Prototype
public class PersonController {

    @Inject
    private DaoFactory daoFactory;    private Dao<Person, Object> personDao;
    public List<Person> personList;

    @PostConstruct
    public void init() {
        try {
            personDao = daoFactory.getDao(Person.class);            personList = dao.queryForAll();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void onAddChuckClicked(){
        Person chuck = new Person("Chuck", "Norris");
        dao.create(chuck);
        personList = dao.queryForAll();
        MobileUI.firePropertyChanged(this, "personList");
    }
}

Further Reading