How to decouple room entity - android

I have room entity like this
#Entity(tableName = "AppUser",
indices = {#Index(value = {"UserId", "UserName"}, unique = true)})
public class AppUser {
#PrimaryKey(autoGenerate = true)
#ColumnInfo(name = "UserId")
public int id;
public AppUser(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
What I want to do is to later add additional column to existing table by extending AppUser class . Something like following
public class ExtendedUser extends AppUser {
public ExtendedUser(int id) {
super(id);
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
#ColumnInfo(name = "address")
String address;
}
So later if someone wants to modify the table , they do not need to modify AppUser , rather they would simply extend it .
Can I achieve it ?

Related

Android room cannot find setter build error when using #PrimaryKey(autoGenerated = true)

I am trying to implement Room database in my apps. And I created a simple model class called "Word".
#Entity(tableName = "word_table")
public class Word {
#PrimaryKey(autoGenerate = true)
private int id;
#NonNull
private String word;
public Word(#NonNull String word) {
this.word = word;
}
public int getId() {
return id;
}
public String getWord(){
return this.word;
}
}
But when I tried to build the apps. It says:
error: Cannot find setter for field.
private int id;
So I tried to add a setter for the "id" myself like:
public void setId(int id) {
this.id = id;
}
The full class looks like this:
#Entity(tableName = "word_table")
public class Word {
#PrimaryKey(autoGenerate = true)
private int id;
#NonNull
private String word;
public Word(#NonNull String word) {
this.word = word;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getWord(){
return this.word;
}
}
But it doesn't resolve the problem. So what is the proper way to use an auto-generated id in room?

How to retrieve a value from list android

I have a list specialties having 75 items that has two values id and name.
private List specialties;
I would like to get the name without using a loop something like below
specialties.get(0).name;
I get an error saying can't resolve name. Is there any way to retrieve name from the values list.
Thanks.
Create a class (Model) to get and set the ID and Name property:-
public class ClassName {
private String id;
private String name;
public ClassName(String mId, String mName){
this.id=mId;
this.name=mName;
}
public String getName() {
return name;
}
public void setName(String sName) {
this.name = sName;
}
public String getId() {
return id;
}
public void setId(String sId) {
this.id = sId;
}
}
In your Activity:-
Define a List having the ClassName type of objects.
List<ClassName> mList = new ArrayList<>();
Now access the property name like this:-
mList.get(0).getName();
mList.get(0).getId();
try the following:
public class Clone {
private int Id;
private String name;
public int getId() {
return Id;
}
public String getName() {
return name;
}
public void setId(int id) {
Id = id;
}
public void setName(String name) {
this.name = name;
}
}
and use
private List<Clone> arrayList = new ArrayList();
for (int i = 0; i < 10; i++) {
Clone helloItem = new Clone();
helloItem.setId(i);
helloItem.setName("I'm Clone no - " + i);
arrayList.add(helloItem);
}
Log.d("check", "get item - " + arrayList.get(0).getName());
hope it help.
I want to suggest you. You should declare name with public access. If it is not public, use public getter and setter method.Call getName() method.
public class YourClass{
private int id;
private String name;
public YourClass(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
private List<YourClass> specialties = new ArrayList<YourClass>;
...//add data into list...
specialties.add(new YourClass( 1 , "John"));
...// retrieve data
specialties.get(position).getName();
You have declared your list as private List specialties; you can not access it like this specialties.get(0).name;
You need declare your list like this private List<YourModelClass> specialties;
SAMPLE DEMO
If you want add model class in your list than than check this example
create model class like this
public class User {
String name, email;
public User(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Now use like this in your list
List<User> userArrayList= new ArrayList<>();
To add data inside list like this
userArrayList.add(new User("Nilesh","abc#gmail.com"));
To get data from your list use like this
userArrayList.get(0).getEmail();
userArrayList.get(0).getEmail();
or
for (int i=0;i<userArrayList.size();i++){
userArrayList.get(i).getName();
userArrayList.get(i).getEmail();
}

Realm optional way for foreign key

I am new to Realm. Actually, Realm doesn't provide us functionality for Foreign key and I want to use the functionality of Foreign key.
So Here is my model :
public class CategoryInfo extends RealmObject {
#SerializedName("name")
String name;
#SerializedName("id")
int id;
#SerializedName("sub_category")
RealmList<SubCategoryInfo> subCategoryList;
public Data() {
}
public Data(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setSubCategory(RealmList<SubCategoryInfo> subCategoryList) {
this.subCategoryList = subCategoryList;
}
public RealmList<SubCategoryInfo> getSubCategoryList() {
return subCategoryList;
}
}
public class SubCategoryInfo extends RealmObject {
#SerializedName("sub_cat_name")
String subCatName;
#SerializedName("sub_id")
int subId;
int catId;
public String getSubCatName() {
return subCatName;
}
public void setSubCatName(String subCatName) {
this.subCatName = subCatName;
}
public int getSubId() {
return subId;
}
public void setSubId(int subId) {
this.subId = subId;
}
public void setCatId(int catId) {
this.catId = catId;
}
}
So now when I am getting a response from API side I am updating my object by below code to add catId in SubCatInfo class.
List<CategoryInfo> catInfoList = catResult.getData();
for(CategoryInfo catInfo:catInfoList){
List<SubCategoryInfo> subCatList = catInfo.getSubCategoryList();
if (subCatList != null) {
for (SubCategoryInfo subCatInfo:SubCategoryInfo) {
subCatInfo.setCatId(catInfo.getId());
}
}
}
So What I want is that when I will delete any category from the main table, subCategory should be automatically deleted. So By what way I can achieve this functionality?
Thank you in advance.

Professional way to handle model classes when using GSON and SQLight Android app

In My application I get data from a web service and display those data in recycler view. After that I'm planing to add those data in to local sqlite database and display those data when user open application without internet connection.
Here's a simple model class I'm using to pars JSON result using GSON
public class Repo implements Parcelable {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("name")
#Expose
private String name;
#SerializedName("url")
#Expose
private String url;
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(this.id);
dest.writeString(this.name);
dest.writeString(this.url);
}
public Repo() {
}
protected Repo(Parcel in) {
this.id = (Integer) in.readValue(Integer.class.getClassLoader());
this.name = in.readString();
this.url = in.readString();
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public String getUrl() {
return url;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setUrl(String url) {
this.url = url;
}
public static final Creator<Repo> CREATOR = new Creator<Repo>() {
#Override
public Repo createFromParcel(Parcel source) {
return new Repo(source);
}
#Override
public Repo[] newArray(int size) {
return new Repo[size];
}
};
}
I can create a almost identical model class to represent SQLite data. In here I'm using ORMlite. But this is very similar situation for other ORMs.
#DatabaseTable(tableName = Repo.TABLE_NAME)
public class Repo {
public static final String TABLE_NAME = "repo";
#DatabaseField(columnName = "repo_id")
private long repoId;
#DatabaseField(columnName = "name")
private String name;
public long getRepoId() {
return repoId;
}
public String getName() {
return name;
}
public void setRepoId(long repoId) {
this.repoId = repoId;
}
public void setName(String name) {
this.name = name;
}
}
But by the time I'm trying to save these data in to SQLite database I already have data objects set in GSON model classes. It's kind a redundant thing copy object from GSON model and setting that values in to SQLite model. So I came up with below solution by trying to use single model class to represent both.
#DatabaseTable(tableName = Repo.TABLE_NAME)
public class Repo implements Parcelable {
public static final String TABLE_NAME = "repo";
#DatabaseField(columnName = "repo_id")
#SerializedName("id")
#Expose
private Integer id;
#DatabaseField(columnName = "name")
#SerializedName("name")
#Expose
private String name;
#SerializedName("url")
#Expose
private String url;
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(this.id);
dest.writeString(this.name);
dest.writeString(this.url);
}
public Repo() {
}
protected Repo(Parcel in) {
this.id = (Integer) in.readValue(Integer.class.getClassLoader());
this.name = in.readString();
this.url = in.readString();
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public String getUrl() {
return url;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setUrl(String url) {
this.url = url;
}
public static final Creator<Repo> CREATOR = new Creator<Repo>() {
#Override
public Repo createFromParcel(Parcel source) {
return new Repo(source);
}
#Override
public Repo[] newArray(int size) {
return new Repo[size];
}
};
}
I have try this with different type of model class where it only had String type fields. Since GSON uses types like Integer,Boolean That stopping me from using same model for SQLite because database does not identify Integer as a type, in order to work it need to be int.
So what is the professional way to handle this ? Don't I have any other option other than going back to the method of creating two separate model class to represent SQLite and GSON.
Yout approach is absolutely correct, but i think you are putting too much effort reinventing the wheel
You can easily achieve the described task using Room

Fetching objects with Collections

I'm trying to list objects that have an inner Collection. I can save and retrieve objects just fine, but when I do:
parentRepo.findAll()
Only the last object has it's child object listed, others has an empty collection.
Parent model
#ForeignCollectionField(eager = false)
private Collection<Child> childs;
Child model
#DatabaseField(foreign=true,foreignAutoRefresh=true)
private Parent parent;
eager true or false doesn't make any difference. If i query a child and get its parent, I can get it's children as well. What am I missing?
Edit:
It's working for the modeling that I made. My mistake was that I need a Many-to-many relation between parent and child. I made a quick research and what I need is an intermediate model to achieve this. I'll close this question and will try to made this many-to-many relation between my models.
I solve my Many-to-Many relationships like this:
This is an example from an ongoing project. I have a Many-to-Many relationship between Preparation and GlideWax. To solve it I use thee classes: Preparation, GlideWax and PreparationGlideWax. PreparationGlideWax represents the connections between the the other classes, just like the way you usually solve many-to-many relationships with a table that is a "link" between the tables in the relationship. As you can see GripWax and Structure also has a Many-to_many relationship to preparation. Here is the code:
GlideWax.java
#DatabaseTable(tableName = "glide_waxes")
public class GlideWax {
#DatabaseField(id = true)
private int id;
#DatabaseField(canBeNull = false)
private String name;
#DatabaseField
private String description;
#DatabaseField(canBeNull = false)
private int inUse;
#DatabaseField(foreign=true)
private WaxBrand waxBrand;
#DatabaseField(foreign=true)
private GlideWaxType glideWaxType;
#ForeignCollectionField
private ForeignCollection<PreparationGlideWax> preparationGlideWaxes;
#ForeignCollectionField
private ForeignCollection<TestSessionGlideWax> testSessionGlideWaxes;
public GlideWax() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getInUse() {
return inUse;
}
public void setInUse(int inUse) {
this.inUse = inUse;
}
public WaxBrand getWaxBrand() {
return waxBrand;
}
public void setWaxBrand(WaxBrand waxBrand) {
this.waxBrand = waxBrand;
}
public GlideWaxType getGlideWaxType() {
return glideWaxType;
}
public void setGlideWaxType(GlideWaxType glideWaxType) {
this.glideWaxType = glideWaxType;
}
public ForeignCollection<PreparationGlideWax> getPreparationGlideWaxes() {
return preparationGlideWaxes;
}
public void setPreparationGlideWaxes(ForeignCollection<PreparationGlideWax> preparationGlideWaxes) {
this.preparationGlideWaxes = preparationGlideWaxes;
}
public ForeignCollection<TestSessionGlideWax> getTestSessionGlideWaxes() {
return testSessionGlideWaxes;
}
public void setTestSessionGlideWaxes(ForeignCollection<TestSessionGlideWax> testSessionGlideWaxes) {
this.testSessionGlideWaxes = testSessionGlideWaxes;
}
}
Preparation.java
#DatabaseTable(tableName = "preparations")
public class Preparation {
#DatabaseField(generatedId=true)
private int id;
#ForeignCollectionField
private ForeignCollection<PreparationGlideWax> preparationGlideWaxes;
#ForeignCollectionField
private ForeignCollection<PreparationGripWax> preparationGripWaxes;
#ForeignCollectionField
private ForeignCollection<PreparationStructure> preparationStructures;
#DatabaseField(foreign=true, canBeNull = false)
private SkiPair skiPair;
#DatabaseField(foreign=true, canBeNull = false)
private SkiTester skiTester;
#DatabaseField(foreign=true)
private Rfid rfid;
#DatabaseField(foreign=true, canBeNull = false)
private TestSession testSession;
#ForeignCollectionField
private ForeignCollection<Measurement> measurements;
public Preparation() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public ForeignCollection<PreparationGlideWax> getPreparationGlideWaxes() {
return preparationGlideWaxes;
}
public void setPreparationGlideWaxes(ForeignCollection<PreparationGlideWax> preparationGlideWaxes) {
this.preparationGlideWaxes = preparationGlideWaxes;
}
public ForeignCollection<PreparationGripWax> getPreparationGripWaxes() {
return preparationGripWaxes;
}
public void setPreparationGripWaxes(ForeignCollection<PreparationGripWax> preparationGripWaxes) {
this.preparationGripWaxes = preparationGripWaxes;
}
public ForeignCollection<PreparationStructure> getPreparationStructures() {
return preparationStructures;
}
public void setPreparationStructures(ForeignCollection<PreparationStructure> preparationStructures) {
this.preparationStructures = preparationStructures;
}
public SkiPair getSkiPair() {
return skiPair;
}
public void setSkiPair(SkiPair skiPair) {
this.skiPair = skiPair;
}
public SkiTester getSkiTester() {
return skiTester;
}
public void setSkiTester(SkiTester skiTester) {
this.skiTester = skiTester;
}
public Rfid getRfid() {
return rfid;
}
public void setRfid(Rfid rfid) {
this.rfid = rfid;
}
public TestSession getTestSession() {
return testSession;
}
public void setTestSession(TestSession testSession) {
this.testSession = testSession;
}
}
PreparationGlideWax.java
#DatabaseTable(tableName = "preparation_glide_wax")
public class PreparationGlideWax {
#DatabaseField(generatedId=true)
private int id;
#DatabaseField(canBeNull = false)
private int layer;
#DatabaseField(foreign=true, canBeNull = false)
private GlideWax glideWax;
#DatabaseField(foreign=true, canBeNull = false)
private Preparation preparation;
public PreparationGlideWax() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getLayer() {
return layer;
}
public void setLayer(int layer) {
this.layer = layer;
}
public GlideWax getGlideWax() {
return glideWax;
}
public void setGlideWax(GlideWax glideWax) {
this.glideWax = glideWax;
}
public Preparation getPreparation() {
return preparation;
}
public void setPreparation(Preparation preparation) {
this.preparation = preparation;
}
}
As I said in the edit, I'm able to load the child from parent just fine. My problem is that I need a many-to-many relation between my models. I'll accept this answer in two days.

Categories

Resources