Simple ORM for Android SQLite
Use the issue tracker to report bugs or request enhancements, and StackOverflow with tag storm-gen for assistance. Join the storm-gen G+ community for project news and feature discussion.
- Download storm-api.jar from the latest release and add to your Android project's libs folder so it will be on the project's build path.
- Download storm-apt.jar from the latest release and add to your annotation factory classpath (in Eclipse, project properties | Java Compiler > Annotation Processing > Factory Path). Also check the box to enable annotation processing.
- Create a new class that extends com.turbomanage.storm.DatabaseHelper (it's in the storm-api jar).
- Add @Database and supply a database name and version example
- Override getUpgradeStrategy() to choose one of the available strategies (DROP_CREATE for new projects)
- Create a POJO class you want to persist to the database
- Make sure it has a field of type long named "id" or annotated with @Id
- Add the @Entity annotation to the class example
You'll see 3 generated classes under .apt_generated (you might need to unfilter hidden resources in your IDE to see it):
- a DbFactory class
- a Dao class
- a Table class
You can use the DAO like this:
PersonDao dao = new PersonDao(getContext());
long id = dao.insert(new Person());
Person person = dao.get(id);
For more info, see the unit tests and the resources on the project home page.
The lib is available on Maven Central. Add the android-apt plugin and stORM dependencies to build.gradle. Log4j and javax.persistence are needed if you use the JPA @Entity annotation instead of stORM's @Entity.
// top-level build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.10.+'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2'
}
}
// app module build.gradle
apply plugin: 'android'
apply plugin: 'android-apt'
...
dependencies {
apt 'com.turbomanage.storm:storm-impl:0.98'
compile 'com.android.support:appcompat-v7:19.1.0'
compile 'log4j:log4j:1.2.17'
compile 'javax.persistence:persistence-api:1.0'
compile 'com.turbomanage.storm:storm-api:0.98'
}