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


Keine Kommentare:

Kommentar veröffentlichen