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.
Related
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 am converting an object to JSON using com.google.code.gson:gson:2.2.4 library by using code:
String json = new GsonBuilder().excludeFieldsWithModifiers(Modifier.PROTECTED).create().toJson(object);
And in the JSON string "serialVersionUID" is added automatically with Long value even if it is not in a model class. I just want to remove serialVersionUID from JSON.
I found this answer. Basically, the serialVersionUID is added by InstantRun, disabling InstantRun solved the issue for me.
One way to get around this is to use GsonBuilder.excludeFieldsWithoutExposeAnnotation then use the #Expose annotation to explicitly mark what is or isn't (de)serialized.
public class SomeClass {
private int field1 = 2;
#Expose private int field2 = 6;
#Expose #SerializedName ("foo") private int field3 = 12;
}
gives you {"field2":6, "foo":12}. The field field1 is excluded because it isn't annotated with #Expose.
Personally, I always use the GsonBuilder.excludeFieldsWithoutExposeAnnotation because it filters out any generated fields (like the Instant Run comment above). If you didn't annotate it with #Expose, it won't be serialized/deserialized.
Another way is to declare the field as transient.
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();
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.