I'm writing an Android application that uses Sugar ORM for my data persistence. I have a case where I am getting an objectID from a web server, and this ID needs to be the same as my record's ID on the local database. That way my data record will be updated correctly when saved later on.
The issue I am having is that whenever I extend the SugarRecord class Sqlite is overwriting the ID I am setting in the object. For example, the ID in my feed is 2 but when inserted into the database the ID is changed to 1.
Is there anyways to prevent the auto incrementing that the database is doing with Sugar ORM?
Thanks!
In sugar ORM primary key with autoincrement property is set with id column by default.Try to update the schema version by entering some higher number in Manifest file and also rename the variable(id variable in POJO class)
I had the same problem that I solved by overwriting the ID setter in my model class like this :
public class MyClass extends SugarRecord {
#Unique
private Long id;
// default constructor for SugarRecord
public MyClass() {
}
#Override
public Long getId() {
return this.id;
}
#Override
public void setId(Long id) {
// Use "super" to overwrite
super.setId(id);
this.id = id;
}
}
Related
When using the android data-persistent library Android Room ,how can I directly insert the Comment Object into the database including all the field value, and how can I query all the value out as a Comment Object?
As I know, I can not use the Comment Object as a Entity in Room directory, because of the field replyComment is also a Comment Object. And I can not query out a Comment Object even I define a POJO using the #Relations annotation either because of the one-to-one relations and one-to-many relations all included in the Comment Object.
Is there any other way except changing the Comment Model definition, such as using foreign key, making a effect on insert action and query action?
public class Comment {
public String content;
public String id;
public Comment replyComment;
public User user;
public List<ImageMedia> images;
}
public class User{
public String id;
public String name;
}
public class ImageMedia{
public String key;
public String url;
}
Is there any other way except changing the Comment Model definition
No. You would need to create a set of entities that model the database structure, where children have foreign key columns pointing back to their parents:
CommentEntity has a foreign key back to CommentEntity for the reply
CommentEntity has a foreign key back to UserEntity
ImageMediaEntity has a foreign key back to CommentEntity
I have just started to use RealmDB and cannot figure out how to save linked object correctly, to implement a sort of foreign key
Here is my main User model.
public class UserModel extends RealmObject {
#PrimaryKey
public Long id;
public String firstName;
public String lastName;
public UserSettings userSettingsModel;
}
UserSettings Model is defined as follows.
public class UserSettingsModel extends RealmObject {
#PrimaryKey
private Long id;
public String email;
public RealmList<Car> cars;
}
And Car is a model itself.
public class Car extends RealmObject {
#PrimaryKey
private Long id;
public String model;
}
The problem is that when I am trying to save UserModel it tries to recreate all objects assigned to it. So before I saving user model I have already creates some Car objects.
I don't need to create them, but to reference like the foreign key in SQL databases. And when I am retrieving a user from the database it should automatically load all related data by primary keys.
Is it possible to achieve such behavior using Realm ?
Thanks for help. Solved this problem by using the copyToRealmOrUpdate method instead of copyToRealm.
You should create a managed object using realm.createObject(clazz, pkValue); if it doesn't exist yet, then set it as value or add it to the RealmList that you get another managed object.
You can also create managed objects from unmanaged objects with copyToRealmOrUpdate() (if the object has a primary key).
And when I am retrieving a user from the database it should automatically load all related data by primary keys.
The RealmList allows access to the related objects, and in fact, is also queryable by calling .where() on it. However, this is not based on primary keys. That feature is tracked under "computed fields".
I am using greenDao in my app to persist complex object.
For example
class A{
id;
name;
List<B> list;
}
class B{
id;
name;
List<C> list;
}
class c{
id;
name;
}
I got the tables with all class fields(for all classes).
Problem:
I can see records for table A(for class A), tables B and C are empty(no records).
I am persisting object of class A like:
A toSave = //class generated by GSON
aDao = daoService.getADao();
aDao.insert(toSave);
Can you please help me to solve this issue?
Also I cannot find in the documentation if is allowed to query dao object and retrieve object of class A with all information/fields automatically?
thanks!
I have found the solution:
GreenDao is not able to persist cascade data - If you say insertOrReplace(tvShow) it will not persist list of episodes and list of moments. In order to perform that you have to persist all data separately.
For example:
TvShowDao.save(tvShows) OR TvShowDao.insertOrUpdate(tvShows);
AND
EpisodesDao.save(episodes) OR EpisodesDao.save(episodes)
AND
MomentDao.save(moments); OR MomentDao.save(moments);
Important:
Note that save...() will do
an insert for entities that have no ID and
will do an update for entities that have an ID set.
GreenDao Git Issue
In other words, you have to persist all data separately.
I use jackson and sugar orm and i have some errors when parsing. The id field is located in the json constantly 0. What can I do to fix it?
This example my class:
#JsonIgnoreProperties(ignoreUnknown = true)
public class JsonScienceEvent extends SugarRecord<JsonScienceEvent>{
#JsonProperty("id")
private String eventId;
public JsonScienceEvent()
public JsonScienceEvent(String eventId){
this.eventId = eventId;
}
public String getEventId(){
return eventId;
}
the fieldid is inherited from the super class SugarRecord<T> along with the setter and getter methods setId(Long id) and getId().
You can override the id field generated by the Sugar library, but as far as i can remember it uses Long type so if you can change from String identifier to Long all should be fine and, this way you can force the library to use the id you're setting with the setter setId(Long id),
Sugar ORM actually creates its own ID field to maintain. If you're not inserting a value into the eventId field when creating a record, then your column is empty.
Try using "getId()" to get the auto incremented ID from the record. Don't forget to cast to a string if that's what you want back!
How can I make an integer or long field to be auto-incremented using annotation.
As we can read in a documentation:
One important thing to note is that ActiveAndroid creates an id field
for your tables. This field is an auto-incrementing primary key.
Maybe accessing auto-generated primary key will be enough for you?
Moreover, if you would like to create custom primary key in you model, you can check solution mentioned in GitHub issue connected with ActiveAndroid, which looks like this:
#Table(name = "Items", id = "clientId")
public class Item extends Model {
#Column(name = "id")
private long id;
}
Then, id field is custom primary key, which will be auto-incremented.
In case of ActiveAndroid ORM you do not need to write id column in model, It will automatic generate auto incremented value and you can simply use it.
I am giving a sample model below-
#Table(name="Items")
public class Item extends Model{
#Column(name="name")
public String name;
}
Instead of
#Table(name="Items")
public class Item extends Model{
#Column(name="Id")
public long id;
#Column(name="name")
public String name;
}
If item is an object of Item then you can simply get id by using
item.getId();
So, the correct model is first one. For reference you can click here.