I have two classes
City class
private int id;
private String cityName;
Person class
private int id;
private String personName;
private long dob;
private City hometown;
I want to know how I can get a list of people who are living in a particular city
My first attempt was this
RealmResults <Person> people = realm.where(Person.class).equalTo("hometown", "(1, Melbourne)").findAll();
When I execute this line I get following error
java.lang.IllegalArgumentException: Field 'hometown': type mismatch. Was STRING, expected LINK
Therefore, I want to know how I can pass a LINK as the second argument in "equalTo()" method.
RealmResults <Person> people = realm.where(Person.class).equalTo("hometown.cityName", "Melbourne").findAll();
Related
I have an app that stores lots of data to work offline as well.
I have three classes, in a hierarchy like;
public class MainGroup
{
private UUID Oid;
private String name;
private Date CreatedOn;
}
-
public class Group
{
private UUID Oid;
private String name;
private Date CreatedOn;
private MainGruop MainGroup;
}
-
public class Product
{
private UUID Oid;
private String name;
private Date CreatedOn;
private MainGruop MainGroup;
private Group Group;
}
( Oid fields are selected as PrimaryKey with realm attribute. )
Let's say, all MainGroup objects were stored in Realm DB. Then, when i'm trying to insert Group objects, with nested MainGroup object but with only its Oid field to link its master, Realm updates the MainProduct record (with given Oid), and clear the other fields as nulls.
In same way, when i'm inserting Product objects and nested objects are includes only Oid, realm updates all fields with nulls.
So, there are more complex and deeply related objects and when i make a request to get JSON from server, i must produce a very big JSON response to keep data.
And mention to insert method; I'm creating java objects with JSON response via GSON and i'm using Realm.copyToRealmOrUpdate(obj); method to insert.
To reduce payload (JSON size, serialize and insertion process), i need to find a way to fix this issue.
Following is Pojo are extended by realm , at first time getting all fields from api and i am using insertOrUpdate() operation of realm to dump data in realm.
ServiceModel.java
#JsonProperty("id")
private String id;
#JsonProperty("title")
private String title;
#JsonProperty("description")
private String description;
#JsonProperty("current_status")
private String currentStatus;
But for next time i am not getting current_status field from response and so my ServiceModel table is getting updated with currentStatus as null. How To make currentStatus to not getting updated if it is not in response.
I had the same problem and here how I solved it. You should select existing Object by id:
ServiceModel existingService = realm.where(ServiceModel.class).equalsTo("id",id).findFirst();
if(existingService !=null){ // check if such record existed, it may be absent
String oldCurrentStatus = existingService.currentStatus();
//set old current status to new API object
}
I had the same problem and as far as I know there is no Realm function or annotation yet for such case.
I have a requirement where I need to store a List in a column in the database. Serializing the list might be an option, but i am not sure if it is the right one.
Also, i want to avoid creating another table to store the list elements and a reference to the original table row.
I am using ORMLite for the database operations.
Its a concept of foreign collection.
You need to create an entity that wraps the String. Something looks like:
#DatabaseTable
public class Student {
#DatabaseField(generatedId = true)
print int id;
#DatabaseField
private String fname;
#DatabaseField
private String lname;
#DatabaseField(foreign = true)
private Address address;
}
Then your Address class would have a ForeignCollection of these Student.
#DatabaseTable
public class Address {
#DatabaseField(generatedId = true)
print int id;
#ForeignCollectionField()
private ForeignCollection<Student> student;
}
Also refer this link , may it will help you.
How to save an ArrayList using ORMlite in android my model is as follows
class Model{
#DatabaseField
public String type = "";
#DatabaseField
public String name = "";
#DatabaseField
public Date dateTime = null;
#DatabaseField
ArrayList<Item> items = null;
}
And Item class has
class Item{
#DatabaseField
String itemName;
...
}
I am getting the following exception :
java.sql.SQLException: ORMLite can't store unknown class class
java.util.ArrayList for field 'items'. Serializable fields must specify
dataType=DataType.SERIALIZABLE
But when i specify my field as
#DatabaseField(dataType = DataType.SERIALIZABLE)
ArrayList<Item> items = null;
The compiler gives a error called field cannot be resolved please help me with this.
I've just changed the error message to be:
ORMLite does not know how to store class
java.util.ArrayList for field 'items'. Use another class, custom persister, or to serialize it use dataType=DataType.SERIALIZABLE
Maybe that makes more sense? ORMLite is trying to say that it doesn't know how to store lists in a field. If you want to store collection of items then maybe you should take a look at the foreign-collection documentation.
I read all documentation about orm lite, also looking on net but didn't find right answers. I want to execute this statement:
SELECT * from Drive where day_number=150;
I have class that represent table:
#DatabaseTable(tableName="Drive")
public class Drive{
#DatabaseField(generatedId=true)
private int drive_id;
#DatabaseField
private String start_time;
#DatabaseField
private String end_time;
#DatabaseField
private String price;
#DatabaseField
private String distance;
#DatabaseField
private String day_number;
#DatabaseField
private String week_number;
#DatabaseField
private String month;
getters and setters ...
}
When i run:
List<Drive> today = getHelper().getVoznjaDao().queryBuilder().where().
eq("day_number","150").query();
I get NullPointerExeption, and it's not because there is no record for 150 in database.
Please help i am desperate.
Your code looks fine in terms of the query-builder logic. I suspect that something to do with your wiring is incorrect however. In your comments you mention that the NPE happens on the query line.
List<Drive> today = getHelper().getVoznjaDao().queryBuilder().where().
eq("day_number","150").query();
Most likely this means that either:
getHelper() is returning a null helper instance
getVoznjaDao() is returning a null DAO
or there are bugs in ORMLite
today being null would not cause a NPE since it is not dereferenced on the line. Certainly a bug in ORMLite is not out of the question but I would first check with a debugger to see if the getHelper() or getVoznjaDao() calls are returning null. I suspect you will find the bug at that point.