Freitag, 8. Mai 2015

APersist - Easy annotate and persist

APersist is another ORM-Framework which works with annotations.
Here I will show you a simple setup example for APersist. The example is a databases of persons.
Please notice that the design is not completed so there will be several changes in future.

You will find the exmaple on APersistExample.

APersist is now available on github: APersist

  1. Import APersist as library-project into you Android-Project
     
  2. You have to create the POJOs youneed for you application.
    In the example we will need a class called Person. This classes we have to annotate with the Annotations provided from APersist. Those are:
    • @PersistenceClass
    • @Table
    • @Column
    • @Id
    Every class we want to persist has to be annotated with PersistenceClass. If you want to save this class in a table with the another name different from the classname, you have to annotatie the class aditionally with Table an set the property name.

    So Person which is save in table Persons will looks like this:

    @PersistenceClass
    @Table(name = "Persons")
    public class Person {
    
     @Id(autoincrement = true)
     private Long id;
    
     @Column
     private String firstname;
    
     @Column
     private String lastname;
    
     public Long getId() {
      return id;
     }
    
     public void setId(Long id) {
      this.id = id;
     }
    
     public String getFirstname() {
      return firstname;
     }
    
     public void setFirstname(String firstname) {
      this.firstname = firstname;
     }
    
     public String getLastname() {
      return lastname;
     }
    
     public void setLastname(String lastname) {
      this.lastname = lastname;
     }
    
    }
    

    Don't forget to generate getters and setters for every annotated field ;-)
  3. To get access of the data in database you need DAOs. So the next step is to implement the PersonDao which is very easy. APersist provides therefore a generic class called DAO. There is just one method you have to implement. This method - getParameterType() - has to return the class-object of the connected PersistenceClass.

    So the PersonDao will look like this:
     
    public class PersonDao extends DAO<Person> {
    
     public PersonDao() {
     }
     
     public PersonDao(DatabaseImpl db) {
      super(db);
     }
    
     @Override
     protected Class getParameterType() {
      return Person.class;
     }
    
    }
    
    }
      
    DON'T FORGETT THE DEFAULT CONSTRUCTOR IN DAO-CLASSES!!!
  4. In the next step you have to register the corresponding Dao and his PersistenceClass in an Registry. Therefore APersist provides another abstract class called DbRegistry. If you inherit from this class you have to implement the method setup. There you can add pairs of Dao and PersistenceClass with the method add.

    So the ExampleDbRegistry will looks like this:

    public class ExampleDbRegistry extends DbRegistry {
    
     @Override
     protected void setup() {
      add(PersonDao.class, Person.class);
     }
    
    }
    
    
  5. In order to create a database you have to call the constructor of DatabaseImpl
  6. new DatabaseImpl(Context, DatabaseName, DbRegistry,  Version);
    

    The variable Context is the application contex. DatabaseName is obviously the name of the databas. The DbRegistry is an isntance of your own registry. And the version is an integer which represents the version number of the database. So if you change anything which influence the structure of the databases, please increment the version variable.

    So in the example it will looks like this:
    public class ExampleApplication extends Application {
    
     private DatabaseImpl db;
     private PersonDao personDao;
     private int version = 1;
    
     public PersonDao getTaskDao() {
      if (personDao == null)
       personDao = new PersonDao(getDB());
      return personDao;
     }
    
     private DatabaseImpl getDB() {
             if (db == null){
                    db = new DatabaseImpl(this,
                                "Example.db", getDbRegistry(),
                                 version);
             }
                    return db;
     }
    
     private DbRegistry getDbRegistry() {
      return new ExampleDbRegistry();
     }
    
    }
     
     
  7. Handle DAOs to insert, delete, select or update data from database.
    In the example you will find the code which accesses the DAOs in the MainActivity. There is one method which deletes a person from database and another which adds a person to database.
     
            private void updateList() {
      ListView personList = 
                            (ListView) findViewById(R.id.list);
      List persons = getApp().getPersonDao().loadAll();
      personList.setAdapter(
                                 new PersonAdapter(this, persons));
      personList.setOnItemLongClickListener(
                                   new DeletePersonListener(this));
     }        
    
            @Override
     public void addPerson(String firstName, String lastName) {
      Person person = new Person();
      person.setFirstname(firstName);
      person.setLastname(lastName);
      getApp().getPersonDao().insertOrUpdate(person);
      updateList();
     }
    
            @Override
     public void delete(Person person) {
      getApp().getPersonDao().delete(person);
      updateList();
     }
    
    

    The other code around is just needed for the app. You also can try it in an emulator and have a look to the database with sqlite3.

Keine Kommentare:

Kommentar veröffentlichen