So, I have two models: Document and Item. Table creation and insertion works just perfect. What I want to know is that if I do something like this:
mDaoSession.getDocumentDao().deleteInTx(selectedDocuments);
//or
mDaoSession.getDocumentDao().deleteByKeyInTx(documentIds);
Will any of queries above delete all Items related to this Document or should I do it manually (with additional code)? If not deleting is there any way in GreenDao to accomplish that?
Document.class
public class Document {
#Id(autoincrement = true)
private Long documentId;
#ToMany(referencedJoinProperty = "id")
public List<Item> items;
}
Item.class
public class Item {
private Long id;
}
Related
hope you have a good day.
I am new to greenDAO, and am wondering how to model a one-to-many relationship using greenDAO in an Android app so that I can retrieve a list of TestResult entities that belong to a User entity. I've used the #ToMany annotation at the User entity class and #ToOne at TestResult, following this documentation. However, even after persisting TestResult entity objects with the User property set, retrieving the same User causes a null error for its list of TestResult entity objects.
I would be greatly appreciate it if you guys would be so gracious as to to provide some help.
Thank you.
#Entity
public class Customer {
#Id private Long id;
#ToMany(referencedJoinProperty = "customerId")
#OrderBy("date ASC")
private List<Order> orders;
}
#Entity
public class Order {
#Id private Long id;
private Date date;
private long customerId;
}
Customer and Order are two entities. One customer have multiple orders. For this customerId is in order entity.
private long customerId;
In customer Entity,
#ToMany(referencedJoinProperty = "customerId")
#OrderBy("date ASC")
private List<Order> orders;
Reference
for details
I have a list view in android where i have to check every time do display the List item or not
to reduce the requests what i did is saved the id in a single row like
1,2,10,
everything was working fine to search i just had to use
String[] favs = fav.split(",");
for (int index = 0; index <(favs.length); index++) {
if(favs[index]==""){}else {
wishlist.add(Integer.parseInt(favs[index].trim()));
}
if(clicklist.contains((int)temp.getId())) //like this
and to remove from db like, this
temp2.replaceAll(""+m1.getId()+",", "") // and save in the db
now issue is i have two more data field associated with id like
10|data1|data2,100|apple|dog,150|data12|data24
Question 1 is this data model ok for small db
Question 2 how to perform search and delete in new data set?
please help!
Using a db is a proper choice here, i suggest you to take a look to the recently released Room, an Android component made by Google developers to support data persistence more easily.
You should of course know the basis of sql language.
In your case you should annotate your data class with #Entity annotation:
#Entity
public class DataModel {
#PrimaryKey
private int uid;
#ColumnInfo(name = "animal")
private String animal;
#ColumnInfo(name = "fruit")
private String fruit;
// Getters and setters are ignored for brevity,
// but they're required for Room to work.
}
And then, to answer your question about CRUD operations, define a Dao:
#Dao
public interface UserDao {
#Query("SELECT * FROM DataModel")
List<DataModel> getAll();
#Insert
void insertAll(DataModel... dataModels);
#Delete
void delete(DataModel dataModel);
}
I have two RealmObjects, something like,
public class Dog extends RealmObject {
#Required
private String name;
private int age;
}
public class Person extends RealmObject {
#Required
private String name;
private Dog dog; // A person has only one dog (a relationship)
}
In one of the activities, I want to let the user select the dog for the given person.
I thought I will use a spinner.
I also got a sample code from Realm-Examples, that did similar stuff for the ListView.
public class DogListAdapter extends RealmBaseAdapter<Dog> implements SpinnerAdapter
So far everything works fine. The spinner list automatically updates to reflect changes in the realm.
But, when I have to first display the GUI, I want to set the selected item to the current Person.dog.name.
What is the best way to do this?
I am doing something like this in onResume():
dogNameSpinner.setSelection(RealmUtils.resultIndexOf(dogsResult, person.getDog()));
where, the RealmUtils.resultIndexOf is a small utility function:
public static int resultIndexOf(RealmResults results, RealmObject obj) {
for(int i=0; i < results.size(); i++ ) {
if(results.get(i) == obj) {
return i;
}
}
return 0;
}
This does not work. I suspect may be because the realm.changeListeners get called and the spinner resets to the first item whenever items are updated.. ??
I suppose there could be a better way to do this as it should be a common model-view use case.
Let's say I have a structure of realm-objects that looks like this -
public class Person extends RealmObject {
#PrimaryKey
private int id;
private String name;
private List<Pet> pets
// Setters, getters...
}
public class Pet extends RealmObject {
private String name;
private MedicalRecord record;
// Setters, getters...
}
public class MedicalRecord extends RealmObject {
private String something;
private String somethingElse;
// Setters, getters...
}
Now I received a new Person object with an existing id (primary-key) and I want to update this person.
So I do something like this -
realm.beginTransaction();
realm.copyToRealmOrUpdate(person);
realm.commitTransaction();
The trouble is that this person's pet list (and the pets' medical records), are still out there in the db. not linked anymore to this person, but still there.
I tried to do this -
Person existingPerson = realm.where(Person .class).equalTo("id", ID).findFirst();
existingPerson.getPets().clear();
But no success there. How can I remove subobjects of realmObjects?
Also, is there a way to define a policy for a realm-object so that it will remove itself once there is no reference to it (it's not linked to any parent-object)?
Now you can, and method was renamed from last commit to realmList.deleteAllFromRealm()
I have the following RealmObject:
public class City extends RealmObject {
private String cityId;
private RealmList<Street> streets;
public String getId() {
return cityId;
}
public void setCityId(String cityId) {
this.cityId = cityId;
}
public RealmList<Street> getStreets() {
return streets;
}
public void setStreets(RealmList<Street> streets) {
this.streets = streets;
}
}
Now having a cityId I need to query streets of particular city. How to do that? What I did try was:
Realm.getInstance(context).where(City.class).equalTo("cityId", someCityId, false)
.findFirst().getStreets().where().findAll()
But this leads into an Exception. I need to display streets in a ListView implementing filtering so I need streets to be RealmResults to use RealmBaseAdapter<Street>.
The proper way would be to have an open Realm instance either opened in your Activity in onCreate() and closed in onDestroy(), or in your custom application class.
Then you can use this realm instance to query the realm
City city = realm.where(City.class).equalTo("cityId", cityId).findFirst();
Then you can access the RealmList<T> like any other list
RealmList<Street> streets = city.getStreets();
Then you can use a recyclerview to get the view for a given index within your streets list.