ORM on Android SQLite and database scheme [closed]

橙三吉。 提交于 2019-12-17 15:35:58

问题


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?


回答1:


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




回答2:


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.




回答3:


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




回答4:


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.)




回答5:


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/




回答6:


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!



来源:https://stackoverflow.com/questions/2973982/orm-on-android-sqlite-and-database-scheme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!