Mittwoch, 10. Juni 2015

APersist - Delete to many relationship

This is a report about fixed bugs in the last weeks.

The most important thing belongs to the ToMany relationship.
The bug: If you delete an child from the list and save the parent, the relatonship to the child was not deleted in the databases.

The APersistExample app shows that APersist is able to handle this now.
You can find it here.


Other bugs: 

Objects with ToOne relationship can not be updated => fixed
Boolean is not supported => fixed
Update throws exception => fixed



Download APersist and try it => APerist
 
 

Sonntag, 24. Mai 2015

APersist - ToMany relationships

Hello, hello.
I have been working on the apersist-orm-framework. And now I want to show you a new feature coming up in the next days.

I am talking about toMany relations in a database.

The example I want to use has also to do with persons, like in the previous post (see: http://androidappexperience.blogspot.de/2015/05/apersist-easy-annotate-and-persist.html)

Now I want to show how you can add telephone numbers to this persons.

Annotations you need to do this:

  • @PersistenceClass
  • @Table
  • @Column
  • @Id
  • @ToMany
So if you read the previous blogpost you will see that there is only one annotation added.
This is the "ToMany" annotation.

If you have a look to the documentation of this annotation, you will see that there is a field you have to fill in. I am talking about the "target()" field. This field returns the persistence class you want to point to in this relationship.


So for the example we already have the persistence classe "Person" and the "PersonDAO". For the telephone number we also need a persistence class and a DAO. (see below)

@PersistenceClass
public class TelephoneNumber {

 @Id(autoincrement = true)
 private Long id;
 
 @Column
 private Long number;

 public Long getId() {
  return id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 public Long getNumber() {
  return number;
 }

 public void setNumber(Long number) {
  this.number = number;
 }
 
 @Override
 public String toString() {
  return String.valueOf(number);
 }
}


public class TelephoneNumberDao extends DAO {

 public TelephoneNumberDao() {
  super();
 }

 public TelephoneNumberDao(Database db) {
  super(db);
 }

 @Override
 protected Class getParameterType() {
  return TelephoneNumber.class;
 }

}


Additionally you have to register the new DAO in the DBRegistry.

public class ExampleDbRegistry extends DbRegistry {

 @Override
 protected void setup() {
  add(PersonDao.class, Person.class);
  add(TelephoneNumberDao.class, TelephoneNumber.class);
 }

}

And the most important step is to connect the "Person" with "TelephoneNumbers". Therefore you create a new field in "Person" This field has to be iterable . You also have to annotate it with "ToMany" like in the listing below.

@PersistenceClass
@Table(name = "Persons")
public class Person {

 @Id(autoincrement = true)
 private Long id;

 @Column
 private String firstname;

 @Column
 private String lastname;
 
 @ToMany(target = TelephoneNumber.class)
 private List teleNumber;
 
 
 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;
 }
 
 public List getTeleNumber() {
  if(teleNumber == null)
   teleNumber = new ArrayList();
  return teleNumber;
 }

 public void setTeleNumber(List teleNumber) {
  this.teleNumber = teleNumber;
 }

 @Override
 public String toString() {
  return firstname + " " + lastname;
 }
}


So if you did all these things you will be able to persist persons with many telephone numbers.
Look at the example app to check out if it works.


Lookup example: Example


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.