Clone wiki

Lime / Home

Lime ORM for Android

Lime is a light-weight Object Relational Mapper (ORM) layer for Android inspired by Microsoft's Entity Framework. Lime simplifies interaction with SQLite data and works seamlessly with content providers and Android's built-in content observers.

Download com.trashlabs.lime-v0.86.jar

Getting Started

First, create a class in your application that extends from DataEntity.

public class Person extends DataEntity {

    @PrimaryKey
    @GeneratedValue
    public int id; 

    public String lastName; 

    public String firstName; 

}

Then, extend SqlDataStore to hold your objects. Simply add public properties that are DataSet collections of the entities you'd like to store.

public class AppDb extends SqlDataStore {

    public DataSet<Person> people = new DataSet<Person>(this, Person.class);

    public AppDb(Context context) {
        super(context, "example.db", 1);
    }

}

Now you can use your store! Create a new entity and use the SqlDataStore.add() method to save data to the database. A SQL db is automatically created and your object is saved.

Person p1 = new Person(); 
p1.lastName = "Harry"; 
p1.firstName = "Dirty";

AppDb db = new AppDb(getContext()); 
db.add(p1);

Now that you've got something in your database let's look at how to get it back out. You write queries in Lime like this:

AppDb db = new AppDb(getContext());
String lastName = "Harry";
Person p1 = db.people.query().where("lastName='?'", lastName).getSingle();

Lime makes object relationships easy. Let's build on the previous example by adding a Car so we can take Lime for a drive.

public class Car extends DataEntity {

    @GeneratedValue
    @PrimaryKey
    public long id; 
    public String make; 
    public String model;
    public long personId; 

    @OneToOne(child=Person.class, parentKey="personId", childKey="id")
    public Person person; 
}

Notice that OneToOne annotation? That's where the magic happens. Lime sees the annotation and automatically creates a left join to load the related object. How do you use it? Just query:

Person p1 = new Person(); 
p1.lastName = "Mouse";
p1.firstName = "Mickey";
db.people.add(p1);

Car c1 = new Car(); 
c1.make = "Honda";
c1.model = "S2000";
c1.personId = p1.id;
db.cars.add(c1);

c1 = db.cars.query().where("make=?", c1.make).getSingle();
Log.d("Lime", c1.person.lastName);  // logs "Mouse"

Using Lime with ContentProviders

Lime can be used to create objects from other content providers. Create a data entity like this:

public class Contact extends DataEntity {

    @Column("contact_id")
    public long contactId;

    @Column("display_name")
    public String displayName; 
}

Then query the CONTACTS content provider and let the Lime DataList do the deserialization work for you.

final String[] projection = new String[] { 
   RawContacts.CONTACT_ID,  
   RawContacts.DISPLAY_NAME_PRIMARY
};

Cursor cursor = getContext().getContentResolver().query(RawContacts.CONTENT_URI, projection,null, null,null); 

List<Contact> contacts = DataList.create(Contact.class, cursor);

for(Contact contact : contacts) {
   Log.d("lime", "name:" + contact.displayName);
}

Updated