How to make a field auto increment in ActiveAndroid ORM - android

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.

Related

Confuse on dealing with relations between objects using Android Room

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

Save linked objects Primary key value already exists Relationships

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".

Sugar ORM overwrites ID from feed

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;
}
}

Jackson + SugarOrm id error

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!

ForeignCollection create or update

How do I create OR update a ForeignCollection in OrmLite?
If I try to simply add an object to a ForeignCollection, the add method acts as a create (insert into) method, but if the object already exists I will get an error about not having a unique primary key. I don't want duplicates to appear with autoincrementing primary keys, so this is fine to get this notice.
If I use the update method, then it will error if there is nothing to update.
It seems that the foreigncollection object doesn't have a way to tell me if an object is already existing in the database.
So is the only way to write a separate query myself, see if each object exists and drop the ones that have changed?
If you are using for example :
#DatabaseTable(tableName = "question")
public class QuestionDb implements Serializable {
#ForeignCollectionField(foreignFieldName = "question", eager = true)
private ForeignCollection<AnswerDb> answers;
}
#DatabaseTable(tableName="answers")
public class AnswerDb implements Serializable{
#DatabaseField (foreign=true,canBeNull=true,columnName=FIELD_QUESTIONID)
private QuestionDb question;
}
You will have to use the function createOrUpdate of the AnswersDB.
answerYouWantToAdd.setQuestion(yourQuestion);
answerDao.createOrUpdate(answerYouWantToAdd);

Categories

Resources