25

I'm looking for a very simple ORM framework working on Android for SQLite. I've been testing ActiveAndroid but none of the example could ever build on Eclipse.
By the way, how do guys implement a "many to many" relationship in SQLite for Android? How do you reflect the cascade regarding deletion of rows and guarantee the database integrity?

JJD
  • 44,755
  • 49
  • 183
  • 309
BlackLabrador
  • 251
  • 1
  • 3
  • 3
  • Please check out **[JDXA](http://softwaretree.com/2015/products/jdxa/jdxa.html)**, a simple, non-intrusive, and flexible ORM for Android. JDXA supports inheritance, one-to-one, one-to-many, and many-to-many relationships. For many-to-many relationship, the deletions of rows cascade upto the intermediate join table in a transaction, maintaining the integrity of the other side of the relationship. See different [Code Snippets](http://softwaretree.com/2015/products/jdxa/code-snippets.html). – Damodar Periwal Sep 30 '15 at 02:17

6 Answers6

27

I am the main author of ORMLite which has a Android backend which makes calls to the native Android OS database APIs to support its ORM functionality. We have a large number of Android developers that are using the framework successfully. See here for more information:

http://ormlite.com/sqlite_java_android_orm.shtml

In terms of many-to-many relationships, ORMLite does not support cascading or any of the more advanced ORM features but there are examples of easy many-to-many implementations:

http://ormlite.com/docs/examples

Gray
  • 108,756
  • 21
  • 270
  • 333
  • Easy to set up but learning all the names for things that aren't what they seem was very annoying. It just doesn't seem to match any general database terminology. Maybe it just seems that way to me because I barely have experience with SQLite. Spent three full days just reading documentation on how this puzzle is put together, couldn't get it to function properly in the end. Not suitable for SQLite in combination with java beginners in my opinion. – G_V Dec 10 '14 at 14:53
  • Can you be more specific as to the problems @G_V? Anything I can do to improve the documentation? I've spent tons of time on it. – Gray Dec 10 '14 at 18:08
  • I can tell and it's most likely because of my inexperience with the original SQLite android libraries, but you have propagated the not-ideal naming of the built in SQLite libraries by using names such as OrmLiteSqliteOpenHelper. Words like Manager and Helper don't really show me, as a beginner, what is going on. I'm a bit short on time this week, but I'll take some time soon to really document where I had trouble because I think your framework is great, but again, as someone fairly new to SQLite + Android I felt like there were some missing steps in how to get it to work. – G_V Dec 11 '14 at 08:20
11

For those still looking for an ORM solution, I released greenDAO some months ago. Several apps in the Android Market already use it. Unlike other Android ORM tools, one of greenDAOs primary design goals was performance. For many operations it should be multiple times faster than other solutions, e.g. it's 4-5 times faster compared to ORMLite for loading entities.

It supports relations. The documentation describes how to use relations and how you could model many-to-many relations.

Delete-cascade is a dangerous thing, and thus unsupported by greenDAO. The safer way is to delete the entities bottom-up inside a transaction.

Markus Junginger
  • 6,578
  • 29
  • 48
  • I tried the greendao, the concept is great that you are generating the entities and dao, but the way you compose the generator is not so smart. – Andy Feb 09 '13 at 00:13
  • @greenrobot any tips on how to go about performing the bottom-up deletion in case of a hierarchy (relations to the same entity)? – Roberto Andrade Feb 18 '13 at 19:17
  • @RobertoAndrade don't see the connection to greenDAO. Use a Set to track already deleted objects? – Markus Junginger Feb 19 '13 at 13:14
  • Tried greendao but it was a pain to set up and it pretty much requires the generation of entity classes and seemingly also the addition of a long id field which has nothing to do with the entity itself, which disrupts generated webservice calls. The idea is nice, but as it is I don't see how it can be used by people who aren't already very experienced with SQLite. The documentation also assumes WAY too much knowledge on specific implementation details in my opinion. That the android team changes these details almost monthly isn't helping either. – G_V Dec 10 '14 at 14:46
10

I wrote a lightweight ORM myself and called it Androrm. As I love Django, the query syntax looks much alike. Please try and give me feedback :)

Webpage: http://androrm.com/

Also on GitHub: https://github.com/androrm/androrm

philgiese
  • 603
  • 6
  • 17
5

Below is how I do it with SORMA - An Android Content Provider solution.

  1. Map your Java POJO to database table:

    @Table(
    name="contact",
    keyColumn="id",
    autoId=true,
    create="create table if not exists contact (" 
    + " id INTEGER primary key autoincrement"
    + ", firstName text"
    + ", lastName text"
    + ", married tinyint"
    + ")"
    )
    public class Contact {
    private Integer id;
    private String firstName;
    private String lastName;
    private boolean married;
    ......
    
  2. Create content provider class:

    import com.gaoshin.sorma.annotation.ContentProvider;
    import com.gaoshin.sorma.annotation.SormaContentProvider;
    
    @ContentProvider(
        version = 1,
        mappingClasses = {
            Contact.class,
            Phone.class 
        }
    )
    public class AddressBookContentProvider extends SormaContentProvider {
    }
    
  3. Define content provider in the AndroidManifest.xml:

    <provider    
    android:authorities="com.gaoshin.sorma.examples.addressbook.AddressBookContentProvider"
    android:name="com.gaoshin.sorma.examples.addressbook.AddressBookContentProvider" />
    
  4. Use the content provider:

    sorma = SORMA.getInstance(getBaseContext(), AddressBookContentProvider.class);
    
    // insert contact
    Contact contact = new Contact();
    contact.setFirstName("fname1");
    contact.setLastName("lname1");
    sorma.insert(contact);
    

Done!

(You can find more info here.)

Jason Plank
  • 2,322
  • 4
  • 29
  • 39
YongJiang Zhang
  • 1,689
  • 2
  • 17
  • 22
3

I use, and am the author of, Mechanoid DB, that provides a sqlite like dsl for generating sqlite backed content providers.

Check it out:

http://www.robotoworks.com/mechanoid-plugin/mechanoid-db/

Ian Warwick
  • 4,744
  • 3
  • 24
  • 27
2

Try SQLiteGen. It's not as feature-rich as hibernate, but you can generate some simple ORM classes with it in Eclipse.

Hope that helps!

Ryan Hayes
  • 5,326
  • 3
  • 41
  • 51