Pergunta

I am trying to store in DB table a class Object, but it seems ormlite doesn't accept the object type, here is my DB table:

Temp_Contacts DB Table

public class Temp_Contacts {

    @DatabaseField(generatedId = true,canBeNull = false)
    private int id;

    @DatabaseField
     private Contacts contacts;

    @DatabaseField
    private Date data_actualizare;

    Temp_Contacts(){}

    public Temp_Contacts(Contacts contacts, Date data_actualizare){
            this.contacts=contacts;
            this.data_actualizare=data_actualizare;
    }

    public Contacts getContacts() {
        return contacts;
    }

    public void setContactsList(Contacts contacts) {
        this.contact = contacts;
    }

    public Date getData_actualizare() {
        return data_actualizare;
    }

    public void setData_actualizare(Date data_actualizare) {
        this.data_actualizare = data_actualizare;
    }
}

Contacts class

public class Contacts {

    private int ID;
    private String name;
    private List<Telefon> numbers;
    private List<Email> emails;

    public String getName() {
        return name;
    }

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

    public List<Telefon> getNumbers() {
        return numbers;
    }

    public void setNumbers(List<Telefon> numbers) {
        this.numbers = numbers;
    }

    public List<Email> getEmails() {
        return emails;
    }

    public void setEmails(List<Email> emails) {
        this.emails = emails;
    }

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }
}

The main purpose of this is to store objects form Contacts class in ormlite DB. I prefer this logic but,i am opened to other suggestions as well.

Foi útil?

Solução

Basically you have to split this into different tables, for ORMLite to handle it properly and have a clean db design.
You would end up having 4 tables: Temp_Contacts, Contacts, Email and Phone:

public class Temp_Contacts {

    @DatabaseField(generatedId = true,canBeNull = false)
    private int id;

    // declare Contacts as a foreign key
    // automatically fetched when Temp_Contacts is loaded
    @DatabaseField(foreign=true, foreignAutoRefresh=true)
    private Contacts contacts;

    @DatabaseField
    private Date data_actualizare;

    ...
 }

Contacts:

public class Contacts {

   @DatabaseField(id = true,canBeNull = false)
   private int ID;

   @DatabaseField
   private String name;

   // Use foreign collections for Telefon and Email
   // that are loaded with Contacts
   @ForeignCollectionField(eager = true)
   private ForeignCollection<Telefon> numbers;

   @ForeignCollectionField(eager = true)
   private ForeignCollection<Email> emails;     

   ...
}

Telefon and Email

public class Telefon {

    // Reference to the Contact object has to be there
    @DatabaseField(canBeNull = false, foreign = true)
    private Contacs contacts;

    ...

}

public class Email {

    // Reference to the Contact object has to be there
    @DatabaseField(canBeNull = false, foreign = true)
    private Contacs contacts;

    ...

}

This corresponds to the following db structure:

Temp_Contacts:

| id | contacts_id | data_actualizare |

Contacts:

| id | name |

Telefon and Email

| contacts_id | otherfields |

For more information on ForeignObjects and ForeignCollections see the ORMLite documentation

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top