0% found this document useful (0 votes)
216 views14 pages

DBFlow ORM Setup for Android Apps

This document provides information on using the DBFlow annotation based ORM library for Android app development. It demonstrates how to initialize DBFlow, define database and model classes annotated with DBFlow annotations, setup associations between models, and access associated models.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
216 views14 pages

DBFlow ORM Setup for Android Apps

This document provides information on using the DBFlow annotation based ORM library for Android app development. It demonstrates how to initialize DBFlow, define database and model classes annotated with DBFlow annotations, setup associations between models, and access associated models.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROFESSIONAL ANDROID

APP DEVELOPMENT
DBFlow
DBFlow
Annotation based ORM
Fast, powerful and simple
Adding support
[Link] (root)
buildscript {
repositories {
// required for this library, don't use mavenCentral()
jcenter()
}
dependencies {
classpath '[Link]:android-apt:1.8'
}
}

allProjects {
repositories {
maven { url "[Link] }
}
}
Adding support
[Link] (app)
apply plugin: '[Link]-apt'

def dbflow_version = "3.0.0-beta3"

dependencies {
apt "[Link]:dbflow-processor:${dbflow_version}"
compile "[Link]:dbflow-core:${dbflow_version}"
compile "[Link]:dbflow:${dbflow_version}"
compile "[Link]:dbflow-sqlcipher:${dbflow_version}"
}
Initialize and define DB
public class ExampleApplication extends Application {
@Override
public void onCreate() {
[Link]();
[Link](this);
}
}

@Database(name = [Link], version = [Link])


public class ColonyDatabase {
public static final String NAME = "Colonies";
public static final int VERSION = 1;
}
Define a table
@Table(database = [Link])
public class Queen extends BaseModel {
@PrimaryKey(autoincrement = true)
long id;

@Column
String name;
}
Table with association
@Table(database = [Link])
public class Ant extends BaseModel {
@PrimaryKey(autoincrement = true)
long id;

@Column
String type;

@Column
boolean isMale;

@ForeignKey(saveForeignKeyModel = false)
ForeignKeyContainer<Queen> queenForeignKeyContainer;
}
Setting up association
@Table(database = [Link])
public class Ant extends BaseModel {
...
public void associateQueen(Queen queen) {
queenForeignKeyContainer =
[Link]([Link]).toForeignKeyContainer(
queen);
}
}
Setting up association
@ModelContainer
@Table(database = [Link])
public class Queen extends BaseModel {
...
// needs to be accessible for DELETE
List<Ant> ants;

@OneToMany(methods = {[Link], [Link]}, variableName = "ants")


public List<Ant> getMyAnts() {
if (ants == null || [Link]()) {
ants = [Link]()
.from([Link])
.where(Ant_Table.queenForeignKeyContainer_id.eq(id))
.queryList();
}
return ants;
}
}
More info
• [Link]

• [Link]

You might also like