I need your help for decide next throuble.
I have two class Order and OrderProduct, in the Order class has property of collection OrderProduct which return empty. I don't understand what problem, because I set eager = true. you can see my code below.
#DatabaseTable(tableName=Order.TABLE_NAME)
public class Order extends Document {
public static final String TABLE_NAME = "order";
#DatabaseField( generatedId = true)
private int id;
#ForeignCollectionField(eager = true)
private ForeignCollection<OrderProduct> products;
private IPrepareOrder iPrepareOrder;
public Order(){
}
public int getId(){
return id;
}
public Collection<OrderProduct> getProducts(){
return products;
}
}
#DatabaseTable(tableName=OrderProduct.TABLE_NAME)
public class OrderProduct {
public static final String TABLE_NAME = "orderProduct";
#DatabaseField(generatedId = true)
private int id;
#DatabaseField(canBeNull = false, foreign = true, foreignAutoRefresh = true)
private Order order;
#DatabaseField(foreignColumnName = "id", canBeNull = false, foreign = true, foreignAutoRefresh = true)
private Product product;
public OrderProduct(){
}
public int getId(){
return id;
}
public Order getOrder(){
return order;
}
public void setOrder(Order order){
this.order = order;
}
}
Related
I have room entity like this
#Entity(tableName = "AppUser",
indices = {#Index(value = {"UserId", "UserName"}, unique = true)})
public class AppUser {
#PrimaryKey(autoGenerate = true)
#ColumnInfo(name = "UserId")
public int id;
public AppUser(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
What I want to do is to later add additional column to existing table by extending AppUser class . Something like following
public class ExtendedUser extends AppUser {
public ExtendedUser(int id) {
super(id);
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
#ColumnInfo(name = "address")
String address;
}
So later if someone wants to modify the table , they do not need to modify AppUser , rather they would simply extend it .
Can I achieve it ?
I am trying to implement Room database in my apps. And I created a simple model class called "Word".
#Entity(tableName = "word_table")
public class Word {
#PrimaryKey(autoGenerate = true)
private int id;
#NonNull
private String word;
public Word(#NonNull String word) {
this.word = word;
}
public int getId() {
return id;
}
public String getWord(){
return this.word;
}
}
But when I tried to build the apps. It says:
error: Cannot find setter for field.
private int id;
So I tried to add a setter for the "id" myself like:
public void setId(int id) {
this.id = id;
}
The full class looks like this:
#Entity(tableName = "word_table")
public class Word {
#PrimaryKey(autoGenerate = true)
private int id;
#NonNull
private String word;
public Word(#NonNull String word) {
this.word = word;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getWord(){
return this.word;
}
}
But it doesn't resolve the problem. So what is the proper way to use an auto-generated id in room?
I'm trying to list objects that have an inner Collection. I can save and retrieve objects just fine, but when I do:
parentRepo.findAll()
Only the last object has it's child object listed, others has an empty collection.
Parent model
#ForeignCollectionField(eager = false)
private Collection<Child> childs;
Child model
#DatabaseField(foreign=true,foreignAutoRefresh=true)
private Parent parent;
eager true or false doesn't make any difference. If i query a child and get its parent, I can get it's children as well. What am I missing?
Edit:
It's working for the modeling that I made. My mistake was that I need a Many-to-many relation between parent and child. I made a quick research and what I need is an intermediate model to achieve this. I'll close this question and will try to made this many-to-many relation between my models.
I solve my Many-to-Many relationships like this:
This is an example from an ongoing project. I have a Many-to-Many relationship between Preparation and GlideWax. To solve it I use thee classes: Preparation, GlideWax and PreparationGlideWax. PreparationGlideWax represents the connections between the the other classes, just like the way you usually solve many-to-many relationships with a table that is a "link" between the tables in the relationship. As you can see GripWax and Structure also has a Many-to_many relationship to preparation. Here is the code:
GlideWax.java
#DatabaseTable(tableName = "glide_waxes")
public class GlideWax {
#DatabaseField(id = true)
private int id;
#DatabaseField(canBeNull = false)
private String name;
#DatabaseField
private String description;
#DatabaseField(canBeNull = false)
private int inUse;
#DatabaseField(foreign=true)
private WaxBrand waxBrand;
#DatabaseField(foreign=true)
private GlideWaxType glideWaxType;
#ForeignCollectionField
private ForeignCollection<PreparationGlideWax> preparationGlideWaxes;
#ForeignCollectionField
private ForeignCollection<TestSessionGlideWax> testSessionGlideWaxes;
public GlideWax() {
}
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 String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getInUse() {
return inUse;
}
public void setInUse(int inUse) {
this.inUse = inUse;
}
public WaxBrand getWaxBrand() {
return waxBrand;
}
public void setWaxBrand(WaxBrand waxBrand) {
this.waxBrand = waxBrand;
}
public GlideWaxType getGlideWaxType() {
return glideWaxType;
}
public void setGlideWaxType(GlideWaxType glideWaxType) {
this.glideWaxType = glideWaxType;
}
public ForeignCollection<PreparationGlideWax> getPreparationGlideWaxes() {
return preparationGlideWaxes;
}
public void setPreparationGlideWaxes(ForeignCollection<PreparationGlideWax> preparationGlideWaxes) {
this.preparationGlideWaxes = preparationGlideWaxes;
}
public ForeignCollection<TestSessionGlideWax> getTestSessionGlideWaxes() {
return testSessionGlideWaxes;
}
public void setTestSessionGlideWaxes(ForeignCollection<TestSessionGlideWax> testSessionGlideWaxes) {
this.testSessionGlideWaxes = testSessionGlideWaxes;
}
}
Preparation.java
#DatabaseTable(tableName = "preparations")
public class Preparation {
#DatabaseField(generatedId=true)
private int id;
#ForeignCollectionField
private ForeignCollection<PreparationGlideWax> preparationGlideWaxes;
#ForeignCollectionField
private ForeignCollection<PreparationGripWax> preparationGripWaxes;
#ForeignCollectionField
private ForeignCollection<PreparationStructure> preparationStructures;
#DatabaseField(foreign=true, canBeNull = false)
private SkiPair skiPair;
#DatabaseField(foreign=true, canBeNull = false)
private SkiTester skiTester;
#DatabaseField(foreign=true)
private Rfid rfid;
#DatabaseField(foreign=true, canBeNull = false)
private TestSession testSession;
#ForeignCollectionField
private ForeignCollection<Measurement> measurements;
public Preparation() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public ForeignCollection<PreparationGlideWax> getPreparationGlideWaxes() {
return preparationGlideWaxes;
}
public void setPreparationGlideWaxes(ForeignCollection<PreparationGlideWax> preparationGlideWaxes) {
this.preparationGlideWaxes = preparationGlideWaxes;
}
public ForeignCollection<PreparationGripWax> getPreparationGripWaxes() {
return preparationGripWaxes;
}
public void setPreparationGripWaxes(ForeignCollection<PreparationGripWax> preparationGripWaxes) {
this.preparationGripWaxes = preparationGripWaxes;
}
public ForeignCollection<PreparationStructure> getPreparationStructures() {
return preparationStructures;
}
public void setPreparationStructures(ForeignCollection<PreparationStructure> preparationStructures) {
this.preparationStructures = preparationStructures;
}
public SkiPair getSkiPair() {
return skiPair;
}
public void setSkiPair(SkiPair skiPair) {
this.skiPair = skiPair;
}
public SkiTester getSkiTester() {
return skiTester;
}
public void setSkiTester(SkiTester skiTester) {
this.skiTester = skiTester;
}
public Rfid getRfid() {
return rfid;
}
public void setRfid(Rfid rfid) {
this.rfid = rfid;
}
public TestSession getTestSession() {
return testSession;
}
public void setTestSession(TestSession testSession) {
this.testSession = testSession;
}
}
PreparationGlideWax.java
#DatabaseTable(tableName = "preparation_glide_wax")
public class PreparationGlideWax {
#DatabaseField(generatedId=true)
private int id;
#DatabaseField(canBeNull = false)
private int layer;
#DatabaseField(foreign=true, canBeNull = false)
private GlideWax glideWax;
#DatabaseField(foreign=true, canBeNull = false)
private Preparation preparation;
public PreparationGlideWax() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getLayer() {
return layer;
}
public void setLayer(int layer) {
this.layer = layer;
}
public GlideWax getGlideWax() {
return glideWax;
}
public void setGlideWax(GlideWax glideWax) {
this.glideWax = glideWax;
}
public Preparation getPreparation() {
return preparation;
}
public void setPreparation(Preparation preparation) {
this.preparation = preparation;
}
}
As I said in the edit, I'm able to load the child from parent just fine. My problem is that I need a many-to-many relation between my models. I'll accept this answer in two days.
I have the following problem , I have two classes related to record everything is fine however when I return the parent object , the related object is not filled out completely .
Below my classes
#DatabaseTable(tableName = "condicaopagamento")
public class CondicaoPagamento {
#DatabaseField(generatedId=true)
private Integer id;
#DatabaseField
private String descricao;
#DatabaseField
private Integer quatidadeParcela;
#DatabaseField
private Integer diasParcelamento;
#DatabaseField
private boolean usarMesComercial;
#DatabaseField
private Integer parcelaArredondamento;
#DatabaseField(canBeNull = true, foreign=true, maxForeignAutoRefreshLevel=3)
private FormaCobranca formaCobranca;
#DatabaseField
private Double desconto;
#DatabaseField
private Double acrescimo;
//#ForeignCollectionField
//private Collection<Cliente> clientes;
public Integer getId() {
return id;
}
public String getDescricao() {
return descricao;
}
public Integer getQuatidadeParcela() {
return quatidadeParcela;
}
public Integer getParcelaArredondamento() {
return parcelaArredondamento;
}
public Integer getDiasParcelamento() {
return diasParcelamento;
}
public FormaCobranca getFormaCobranca() {
return formaCobranca;
}
public Double getDesconto() {
return desconto;
}
// public Collection<Cliente> getClientes() { return clientes; }
public Double getAcrescimo() {
return acrescimo;
}
public boolean isUsarMesComercial() {
return usarMesComercial;
}
public void setId(Integer id) {
this.id = id;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public void setQuatidadeParcela(Integer quatidadeParcela) {
this.quatidadeParcela = quatidadeParcela;
}
public void setParcelaArredondamento(Integer parcelaArredondamento) {
this.parcelaArredondamento = parcelaArredondamento;
}
public void setUsarMesComercial(boolean usarMesComercial) {
this.usarMesComercial = usarMesComercial;
}
public void setFormaCobranca(FormaCobranca formaCobranca) {
this.formaCobranca = formaCobranca;
}
public void setDiasParcelamento(Integer diasParcelamento) {
this.diasParcelamento = diasParcelamento;
}
public void setAcrescimo(Double acrescimo) {
this.acrescimo = acrescimo;
}
public void setDesconto(Double desconto) {
this.desconto = desconto;
}
// public void setClientes(Collection<Cliente> clientes) {
// this.clientes = clientes;
// }
}
#DatabaseTable(tableName = "formacobranca")
public class FormaCobranca {
#DatabaseField(id = true)
private Integer id;
#DatabaseField
private String descricao;
public Integer getId() {
return id;
}
public String getDescricao() {
return descricao;
}
public void setId(Integer id) {
this.id = id;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
}
FormaCobranca fc = new FormaCobranca();
fc.setDescricao("FORMA 1");
DatabaseHelper helper = new DatabaseHelper(getContext());
FormaCobrancaDao daoFC = new FormaCobrancaDao(helper.getConnectionSource());
daoFC.createIfNotExists(fc);
FormaCobranca persistidoFC = (FormaCobranca)daoFC.queryForEq("id", 1).get(0);
CondicaoPagamento cp = new CondicaoPagamento();
cp.setDescricao("Condicao2");
cp.setFormaCobranca(persistidoFC);
CondicaoPagamentoDao daoCP = new CondicaoPagamentoDao(helper.getConnectionSource());
daoCP.createIfNotExists(cp);
//CondicaoPagamento persistido = (CondicaoPagamento)daoCP.queryForEq("descricao", "Condicao2").get(0);
CondicaoPagamento persistido = (CondicaoPagamento)daoCP.queryForId(1);
FormaCobranca pFC = persistido.getFormaCobranca();
In the pFC Property Description THIS coming " NULL " .
You forget to specify foreignAutoRefresh=true for #DatabaseField annotation.
Furthermore read this answer https://stackoverflow.com/a/14466145/3802890
I am trying to save the arraylist of class objects into the ormlite database , but it is giving the error , java.lang.IllegalArgumentException: No fields have a DatabaseField annotation in class java.util.ArrayList
my code is
#DatabaseTable
public class ManageModelDetails {
#DatabaseField(generatedId = true)
private int id;
#DatabaseField(foreign = true, foreignAutoRefresh = true)
private ArrayList<ModelDetails> listModelDetails;
// ===============================================
public ManageModelDetails() {
super();
}
// ===============================================
public ManageModelDetails(int id, ArrayList<ModelDetails> listModelDetails) {
super();
this.id = id;
this.listModelDetails = listModelDetails;
}
// ===============================================
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setModelList(ArrayList<ModelDetails> listModelDetails) {
this.listModelDetails = listModelDetails;
}
public ArrayList<ModelDetails> getModelList() {
return listModelDetails;
}
}
I think you need to use Foreign Collections. Take a look here:
Foreign Collections
Another similar question
If you want to save an ArrayList of objects to ORMLite the easiest way is this:
#DatabaseField(dataType = DataType.SERIALIZABLE)
private SerializedList<MyObject> myObjects;
and to get my list of objects:
public List<MyObject> getMyObjects() {
return myObjects;
}
and here is SerializedList:
public class SerializedList<E> extends ArrayList<E> implements Serializable {
}