ORMLite user data persister with multiple columns

Say I have a class with a name Personthat is usually stored in a db table using ORMLite.

Now I have a member inside this class Personcalled House. The class Housecontains 3 properties and NEVER is stored in the database.

I want that whenever it is Personsaved, I want to save 3 fields Housein a table Personin 3 different columns.

So, Person table will have: { person_name, person_contact, house_address, house_type, house_date }.

The last 3 fields will come from the object House.

I think I should use DataPersisterfor a member variable House, but does this mean that it will write the entire object Houseto the ONE column? I want to split it into 3 columns inside a table Person. Can anyone help?

Thank!

+5
source share
3 answers

What you request is called nested objects. All fields in the sub-object are stored in the parent object.

, (12/2012) ORMLite . TODO, . @user999717, , . House , ORMLite , Person. :

http://ormlite.com/docs/foreign

+6

Serializable, :

@DatabaseField (dataType = DataType.SERIALIZABLE) 
public Person person;

.

+6

:

@DatabaseField (useGetSet = true)
private String personStr; // Dummy

public Person person;
public String getPersonStr(){ person.toString(); }
public void setPersonStr(String personstr){ person = Person(personstr); }

, , :

@DatabaseField (dataType = DataType.String, useGetSet = truepublic Person person;
public String getPerson(){ person.toString(); }
public void setPerson(String personstr){ person = Person(personstr); }

You can also use dataType = DataType.SERIALIZABLE as indicated in other answers, but using custom serialization, which means actually implementing Serializable and processing all possible versions of serialization when reading. Serial serialization should NOT be used by default, since you will have a corrupted database.

+2
source

All Articles