I'm new to android and ormlite and i have creating an database CRUD operation with One to Many Relationship. The problem is i can't save the Collection data from Parent Class to child collections data into database. Here is my code :
(Parent class)
#DatabaseTable
public class Schedule implements Serializable {
private static final long serialVersionUID = 1L;
#DatabaseField(generatedId = true)
public int id;
#DatabaseField
public Date date;
#DatabaseField
public String patient;
#DatabaseField
public String room;
#ForeignCollectionField(eager = false)
public Collection<Task> tasks = new ArrayList<Task>();
public Schedule() {
}
}
(Child Class)
#DatabaseTable
public class Task implements Serializable {
private static final long serialVersionUID = 1L;
#DatabaseField(generatedId = true)
public int id;
#DatabaseField
public Date time;
#DatabaseField
public boolean done;
#DatabaseField
public String note;
#DatabaseField(foreign=true,foreignAutoRefresh=true)
public Schedule schedule = new Schedule();
public Task() {
}
}
When i save the data (Schedule class) with collection (tasks), it don't save the collection. Am i missing something ?
Thanks.
When i save the data (Schedule class) with collection (tasks), it don't save the collection. Am i missing something?
So there is no easy way for ORMLite to know that your collection of tasks has not already been inserted into the database. If you need to create them they you should either do that yourself or initialize the tasks field with:
parentDao.assignEmptyForeignCollection(parent, "tasks");
This will turn your tasks field into a foreign collection so any add(...) calls to it will be added also to the database.
Related
i have ticket class and message class like below.
i want set change listener just for inner message list in special ticket id no whole table message.
i read about result and object changeListener and Notifications but i can't find any solution ;
how can I do that ?
public class TicketPojo extends RealmObject {
private Date createAt;
private Date updateAt;
#PrimaryKey
private int id;
private String supporterName;
private int supporterID;
RealmList<MessagePojo> messages;
//....
}
and
public class MessagePojo extends RealmObject {
#PrimaryKey
int id;
String messageType;
String Message;
Date createAt;
Date updateAt;
//...
}
I am making an Android application using GreenDao, and I have these two entities:
#Entity
public class Quiz {
#Id(autoincrement = true)
private Long id;
private Date date;
private String type;
#ToMany(referencedJoinProperty = "quizId")
private List<Answer> answers;
}
#Entity
public class Answer {
#Id(autoincrement = true)
private Long id;
private int answer;
private float value;
private int questionNumber;
private String type;
private Long quizId;
}
I'm trying to fetch the list of Quiz using the following code:
DaoSession daoSession = AndroidAdapter.getDaoSession();
QuizDao quizDao = daoSession.getQuizDao();
List<Quiz> quizs = quizDao.loadAll();
But the list of answers always comes empty, what am I doing wrong?
My AndroidAdapter class:
public class AndroidAdapter {
public static Context getContext() {
return AppApplication.getContext();
}
public static DaoSession getDaoSession() {
return AppApplication.getDaoSession();
}
}
And AppApplication class
#Override
public void onCreate() {
super.onCreate();
context = this;
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,ENCRYPTED ? "exam-db-encrypted" : "exam-db");
Database db = ENCRYPTED ? helper.getEncryptedWritableDb("exam-secret") : helper.getWritableDb();
daoSession = new DaoMaster(db).newSession();
this.addAllEvents();
}
public static DaoSession getDaoSession() {
return daoSession;
}
For me, I've not entered some Answer entities in my db.
Once I've added a code to add those entities to the db and I've requested the list with a generated getter for the List<Answer> answers - it all worked as it should 😊
I have a model
public class Response extends RealmObject {
#PrimaryKey
private String _id;
private RealmList<TaskResponse> task_responses;
private AssignmentRecord assignmentRecord;
private ResponseStatistic responseStats;
}
Now, i am trying to save task_responses realmlist inside the Response but couldn't succeed, below is the relevant code:
mRealm.beginTransaction();
response.setTaskResponses(taskResponses);
response.setAssignmentRecord(assignmentRecord);
response.setResponseStats(responseStatistic);
mRealm.commitTransaction();
When I placed debugger before this line
response.setTaskResponses(taskResponses);
the size of taskResponses was 5 but after execution it becomes empty and realm stores a empty list.
TaskResponse Model :
public class TaskResponse extends RealmObject {
#PrimaryKey
private String _id;
private String taskId;
private Boolean answered;
private String answer;
private TaskStatistic taskStat;
}
You can save like this it will work
Realm mRealm = Realm.getDefaultInstance();
mRealm.beginTransaction();
RealmList<TaskResponse> taskResponseslist = new RealmList();
taskResponseslist.addAll(taskResponses);
response.setTaskResponses(taskResponseslist);
response.setAssignmentRecord(assignmentRecord);
response.setResponseStats(responseStatistic);
mRealm.commitTransaction();
mRealm.close();
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 {
}
I have an object that I call BaseObject which all classes that are persisted extend. This class has an _id field, dateCreated, dateModified, etc.... The database tables that correspond to all objects that extends this base object obviously have columns that store these values.
How can I set up these fields using annotations so all these fields are persisted to the database? Can I do this or will I need to add these fields to the POJOs directly? I have included below my base object and one of the classes that extends it.
public class BaseObject {
private int _id;
private String dateCreated;
private String dateModified;
private String createModule;
private String modifiedModule;
// getters and setters here ...
}
Here's the super-class:
#DatabaseTable(tableName = "FOOD")
public class Food extends BaseObject {
public Food(int foodGroupId, String longDescription) {
this.foodGroupId = foodGroupId;
this.longDescription = longDescription;
}
private Integer foodGroupId;
private String longDescription;
// getters and setters here ...
}
Any help would be appreciated. Thank you.
This should work well with ORMLite as long as you annotate each of the fields in both the base-class and the super-class that you want persisted. For example, you need to need to add something like the following annotations to the BaseObject.
#DatabaseField(generatedId = true)
private int _id;
#DatabaseField
private String dateCreated;
#DatabaseField
private String dateModified;
#DatabaseField
private String createModule;
#DatabaseField
private String modifiedModule;
And then you add the following annotations to the super-class:
#DatabaseTable(tableName = "FOOD")
public class Food extends BaseObject {
#DatabaseField
private Integer foodGroupId;
#DatabaseField
private String longDescription;