Hi there
I'm using OrmLite to persist object in my database and i've found some
problems
I have two classes A and B ( B is the child of A) i want to persist A
without persisting its child's manually ( i want this to happen
automatically )
OrmLite's Official documentation says that we can do that by
setting(foreignAutoCreate = true) and here is the link :

http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/field/DatabaseField.html#foreignAutoCreate()

but it did not work for me.

Here is my Two classes


public class Hobby {

       @DatabaseField(generatedId = true)
       private int id;
       @DatabaseField
       private String name ;
       @DatabaseField(foreign = true, foreignAutoCreate = true,
foreignAutoRefresh = true)
       private Person person;

       public Hobby() {
               // TODO Auto-generated constructor stub
       }

       public int getId() {
               return id;
       }

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

       public String getName() {
               return name;
       }

       public void setName(String name) {
               this.name = name;
       }

       public Person getPerson() {
               return person;
       }

       public void setPerson(Person person) {
               this.person = person;
       }

}

______________________________________________________________________________

@DatabaseTable
public class Person {

       @DatabaseField(generatedId = true)
       private int id;
       @DatabaseField(canBeNull = false)
       private String fName;
       @DatabaseField(canBeNull = false)
       private String lName;
       @DatabaseField(canBeNull = false)
       private Date birthDay;
       @DatabaseField(canBeNull = false)
       private String phone;
       @DatabaseField(canBeNull = false)
       private String email;
       @DatabaseField(canBeNull = false)
       private String adress;
       @DatabaseField(canBeNull = false)
       private int postalCode;
       @ForeignCollectionField(eager = false)
       private Collection<Hobby> hobbies;

       public Person() {

       }

       public int getId() {
               return id;
       }

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

       public String getfName() {
               return fName;
       }

       public void setfName(String fName) {
               this.fName = fName;
       }

       public String getlName() {
               return lName;
       }

       public void setlName(String lName) {
               this.lName = lName;
       }

       public Date getBirthDay() {
               return birthDay;
       }

       public void setBirthDay(Date birthDay) {
               this.birthDay = birthDay;
       }

       public String getPhone() {
               return phone;
       }

       public void setPhone(String phone) {
               this.phone = phone;
       }

       public String getEmail() {
               return email;
       }

       public void setEmail(String email) {
               this.email = email;
       }

       public String getAdress() {
               return adress;
       }

       public void setAdress(String adress) {
               this.adress = adress;
       }

       public int getPostalCode() {
               return postalCode;
       }

       public void setPostalCode(int postalCode) {
               this.postalCode = postalCode;
       }

       public void setHobbies(Collection<Hobby> hobbies) {
               this.hobbies = hobbies;
       }

       public ArrayList<Hobby> getHobbies() {
               ArrayList<Hobby> hobbies = new ArrayList<Hobby>();

               for (Hobby hobby : this.hobbies)
                       hobbies.add(hobby);

               return hobbies;
       }

       public void setHobbies(List<Hobby> hobbies) {
               for (Hobby hobby : hobbies) {
                       this.hobbies.add(hobby);
               }
       }

}


____________________________________________________________________________________


This is the main class



public class Main extends Activity {
       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.main);
               DataBaseHelper dbHelper = new DataBaseHelper(this, "",
null, 0);
               Person p = new Person();
               p.setfName("fname goes here");
               p.setlName("lname goes here");
               List<Hobby> hobbiesList = new ArrayList<Hobby>();
               for (int i = 0; i < 5; i++) {
                       Hobby h = new Hobby();
                       h.setName("hobby " + i);

                       hobbiesList.add(h);

               }
               p.setHobbies(hobbiesList);

               try {
                       dbHelper.getDao(Person.class).create(p);

               } catch (SQLException e) {

                       e.printStackTrace();
               }

       }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to