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 {
}
Related
I'm thinking about it, but I can't print the name and age of the json Crospromotion on the console. I'm using the GSON library, but I'm stuck right now and I don't know how to move forward.
I have my JSON
{
"crospromotion": [
{
"name": "Orus",
"age":14
},
{
"name": "Michelle",
"age":29
},
{
"name": "Jack",
"age":29
}
],
"totalAccessorios": 20,
"totalCaras": 20
}
My JSon controller class:
public class Data {
private Crospromotion crospromotion;
private int totalAccessorios, totalCaras;
public int getTotalAccessorios() {
return totalAccessorios;
}
public int getTotalCaras() {
return totalCaras;
}
}
Class Crospromotion:
public class Crospromotion {
private String nombre;
private int edad;
public String getNombre() {
return nombre;
}
public int getEdad() {
return edad;
}
}
MainActivity:
Gson gson = new Gson();
final Data data = gson.fromJson(myResponse, Data.class);
Log.d("MEN: ", data.getTotalAccessorios());
// I want to print in Log, the fields Crospromotion: name and age. But I don't know how to continue.
The attributes in the json string have different names than in the class Crospromotion. So you can just change the property names of the Crospromotion class like this:
public class Crospromotion {
private String name;
private int age;
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
Also in the json string you have an array of Crospromotion instances, but in the data class you only have one instance. That means you have to change the json string or write the class like this.
import java.util.List;
public class Data {
private List<Crospromotion> crospromotions;
private int totalAccessorios, totalCaras;
public int getTotalAccessorios() {
return totalAccessorios;
}
public int getTotalCaras() {
return totalCaras;
}
public List<Crospromotion> getCrospromotions() {
return crospromotions;
}
}
You also didn't have a getter for the Crospromotion List. Therefore you couldn't access it, because the property was private. With this code you should be able to access the data that you want to access.
It looks like you just forgot to add a getter for your Crospromotion field in your Data class?
public class Data {
private List<Crospromotion> crospromotion;
private int totalAccessorios, totalCaras;
public int getTotalAccessorios() {
return totalAccessorios;
}
public int getTotalCaras() {
return totalCaras;
}
public Crospromotion getCrospromotion() {
return crospromotion;
}
}
...
Log.d("MEN: ", data.getCrospromotion().getAge());
Btw, it looks like the fields in your JSON don't match your Crospromotion class. GSON uses reflection to match json fields to members of a class. You can use #SerializedName() to map JSON field names to member fields if the names differ.
For example:
public class Crospromotion {
#SerializedName("name") private String nombre;
#SerializedName("age") private int edad;
public String getNombre() {
return nombre;
}
public int getEdad() {
return edad;
}
}
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 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;
}
}
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 need deserialize a string(json) to a arraylist inside a model. I'm using the Jackson-Annotation library to do this. Anyone can help me?
I've tried this, but doesn't work:
#JsonDeserialize(as = Model.class)
private ArrayList<Model> model;
or:
#JsonDeserialize(as = ArrayModel.class) //ArrayModel extends arrayList<Model>
private ArrayList<Model> model;
Sample:
public class Model extends BaseModel {
#JsonProperty("id")
private int id;
#JsonDeserialize(as = ModelTwo.class)
private ArrayList<ModelTwo> modelTwo;
public ArrayList<ModelTwo> getModelTwo() {
return modelTwo;
}
public void setModelTwo(ArrayList<ModelTwo> modelTwo) {
this.modelTwo = modelTwo;
}
}
I've solved this!
You need say the type of Object and the type of content.
After this, you need create a new Json with properties on params.
On first model:
#JsonProperty("property")
#JsonDeserialize(as=ArrayList.class, contentAs=ModelTwo.class)
private List<ModelTwo> modelsTwo;
On second model:
#JsonCreator
public ModelTwo(
#JsonProperty("id") int id,
#JsonProperty("name") String name) {
this.id = id;
this.name = name;
}