How to use entity class which extends another class in a Room?
up vote
1
down vote
favorite
I tried searching everything but i didn't find any solution. One question is similar but no one has answered it properly. Moving to the question, I have a class which has room entity annotation and the same class extends another class (which is Library class). Now when i run the project I get an errors like Error:Cannot find getter for field. Library's super class making issue here as it works when i remove it. Please tell me how can i make room work with subclass as entity extending super class. Thank you for your time.
@Entity(tableName = "event")
public class EventDetails extends ParentEntity implements Parcelable{
@PrimaryKey(autoGenerate = true)
int id;
String name;
Calendar startDay = Calendar.getInstance();
Calendar endDay = Calendar.getInstance();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Calendar getStartDay() {
return startDay;
}
public void setStartDay(Calendar startDay) {
this.startDay = startDay;
}
public Calendar getEndDay() {
return endDay;
}
public void setEndDay(Calendar endDay) {
this.endDay = endDay;
}
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
}
public class ParentEntity {
private Calendar mDay;
public Calendar getmDay() {
return mDay;
}
public void setmDay(Calendar mDay) {
this.mDay = mDay;
}
}
java android inheritance android-room
|
show 6 more comments
up vote
1
down vote
favorite
I tried searching everything but i didn't find any solution. One question is similar but no one has answered it properly. Moving to the question, I have a class which has room entity annotation and the same class extends another class (which is Library class). Now when i run the project I get an errors like Error:Cannot find getter for field. Library's super class making issue here as it works when i remove it. Please tell me how can i make room work with subclass as entity extending super class. Thank you for your time.
@Entity(tableName = "event")
public class EventDetails extends ParentEntity implements Parcelable{
@PrimaryKey(autoGenerate = true)
int id;
String name;
Calendar startDay = Calendar.getInstance();
Calendar endDay = Calendar.getInstance();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Calendar getStartDay() {
return startDay;
}
public void setStartDay(Calendar startDay) {
this.startDay = startDay;
}
public Calendar getEndDay() {
return endDay;
}
public void setEndDay(Calendar endDay) {
this.endDay = endDay;
}
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
}
public class ParentEntity {
private Calendar mDay;
public Calendar getmDay() {
return mDay;
}
public void setmDay(Calendar mDay) {
this.mDay = mDay;
}
}
java android inheritance android-room
Just extending from another class seems to be no problem for an Entity in my sample app. Please provide more details
– 0X0nosugar
Nov 8 at 18:14
Hi @0x0nosugar, super class has few fields which are private. Because of these, it is throwing an error.
– TechHelper
Nov 8 at 18:23
I added a private field with getter and setter to the super class - still no problem. Do the private fields in your super class have getters (and setters) which are named correctly?
– 0X0nosugar
Nov 8 at 18:28
Yes the names are proper as i have generated them using alt+insert shortcut. Can we force room to only consider subclass which is entity and not it's super class?
– TechHelper
Nov 8 at 18:30
Maybe the problem is that your super class is in a library not in the module itself? (Sorry but I can't verify that immediately.) What happens if you first create a class in the module which extends from "the super class" and then you let your entity extend from the middle class?
– 0X0nosugar
Nov 8 at 18:34
|
show 6 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I tried searching everything but i didn't find any solution. One question is similar but no one has answered it properly. Moving to the question, I have a class which has room entity annotation and the same class extends another class (which is Library class). Now when i run the project I get an errors like Error:Cannot find getter for field. Library's super class making issue here as it works when i remove it. Please tell me how can i make room work with subclass as entity extending super class. Thank you for your time.
@Entity(tableName = "event")
public class EventDetails extends ParentEntity implements Parcelable{
@PrimaryKey(autoGenerate = true)
int id;
String name;
Calendar startDay = Calendar.getInstance();
Calendar endDay = Calendar.getInstance();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Calendar getStartDay() {
return startDay;
}
public void setStartDay(Calendar startDay) {
this.startDay = startDay;
}
public Calendar getEndDay() {
return endDay;
}
public void setEndDay(Calendar endDay) {
this.endDay = endDay;
}
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
}
public class ParentEntity {
private Calendar mDay;
public Calendar getmDay() {
return mDay;
}
public void setmDay(Calendar mDay) {
this.mDay = mDay;
}
}
java android inheritance android-room
I tried searching everything but i didn't find any solution. One question is similar but no one has answered it properly. Moving to the question, I have a class which has room entity annotation and the same class extends another class (which is Library class). Now when i run the project I get an errors like Error:Cannot find getter for field. Library's super class making issue here as it works when i remove it. Please tell me how can i make room work with subclass as entity extending super class. Thank you for your time.
@Entity(tableName = "event")
public class EventDetails extends ParentEntity implements Parcelable{
@PrimaryKey(autoGenerate = true)
int id;
String name;
Calendar startDay = Calendar.getInstance();
Calendar endDay = Calendar.getInstance();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Calendar getStartDay() {
return startDay;
}
public void setStartDay(Calendar startDay) {
this.startDay = startDay;
}
public Calendar getEndDay() {
return endDay;
}
public void setEndDay(Calendar endDay) {
this.endDay = endDay;
}
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
}
public class ParentEntity {
private Calendar mDay;
public Calendar getmDay() {
return mDay;
}
public void setmDay(Calendar mDay) {
this.mDay = mDay;
}
}
java android inheritance android-room
java android inheritance android-room
edited Nov 8 at 19:03
asked Nov 8 at 17:51
TechHelper
561721
561721
Just extending from another class seems to be no problem for an Entity in my sample app. Please provide more details
– 0X0nosugar
Nov 8 at 18:14
Hi @0x0nosugar, super class has few fields which are private. Because of these, it is throwing an error.
– TechHelper
Nov 8 at 18:23
I added a private field with getter and setter to the super class - still no problem. Do the private fields in your super class have getters (and setters) which are named correctly?
– 0X0nosugar
Nov 8 at 18:28
Yes the names are proper as i have generated them using alt+insert shortcut. Can we force room to only consider subclass which is entity and not it's super class?
– TechHelper
Nov 8 at 18:30
Maybe the problem is that your super class is in a library not in the module itself? (Sorry but I can't verify that immediately.) What happens if you first create a class in the module which extends from "the super class" and then you let your entity extend from the middle class?
– 0X0nosugar
Nov 8 at 18:34
|
show 6 more comments
Just extending from another class seems to be no problem for an Entity in my sample app. Please provide more details
– 0X0nosugar
Nov 8 at 18:14
Hi @0x0nosugar, super class has few fields which are private. Because of these, it is throwing an error.
– TechHelper
Nov 8 at 18:23
I added a private field with getter and setter to the super class - still no problem. Do the private fields in your super class have getters (and setters) which are named correctly?
– 0X0nosugar
Nov 8 at 18:28
Yes the names are proper as i have generated them using alt+insert shortcut. Can we force room to only consider subclass which is entity and not it's super class?
– TechHelper
Nov 8 at 18:30
Maybe the problem is that your super class is in a library not in the module itself? (Sorry but I can't verify that immediately.) What happens if you first create a class in the module which extends from "the super class" and then you let your entity extend from the middle class?
– 0X0nosugar
Nov 8 at 18:34
Just extending from another class seems to be no problem for an Entity in my sample app. Please provide more details
– 0X0nosugar
Nov 8 at 18:14
Just extending from another class seems to be no problem for an Entity in my sample app. Please provide more details
– 0X0nosugar
Nov 8 at 18:14
Hi @0x0nosugar, super class has few fields which are private. Because of these, it is throwing an error.
– TechHelper
Nov 8 at 18:23
Hi @0x0nosugar, super class has few fields which are private. Because of these, it is throwing an error.
– TechHelper
Nov 8 at 18:23
I added a private field with getter and setter to the super class - still no problem. Do the private fields in your super class have getters (and setters) which are named correctly?
– 0X0nosugar
Nov 8 at 18:28
I added a private field with getter and setter to the super class - still no problem. Do the private fields in your super class have getters (and setters) which are named correctly?
– 0X0nosugar
Nov 8 at 18:28
Yes the names are proper as i have generated them using alt+insert shortcut. Can we force room to only consider subclass which is entity and not it's super class?
– TechHelper
Nov 8 at 18:30
Yes the names are proper as i have generated them using alt+insert shortcut. Can we force room to only consider subclass which is entity and not it's super class?
– TechHelper
Nov 8 at 18:30
Maybe the problem is that your super class is in a library not in the module itself? (Sorry but I can't verify that immediately.) What happens if you first create a class in the module which extends from "the super class" and then you let your entity extend from the middle class?
– 0X0nosugar
Nov 8 at 18:34
Maybe the problem is that your super class is in a library not in the module itself? (Sorry but I can't verify that immediately.) What happens if you first create a class in the module which extends from "the super class" and then you let your entity extend from the middle class?
– 0X0nosugar
Nov 8 at 18:34
|
show 6 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
This seems to be mainly a naming problem: after changing the field name from mDay to just day and renaming the setter/ getter accordingly, everything compiles.
It looks like the method names which Android Studio generates from mDay are not the ones which Room is looking for.
Since you are in a position to use different field names in the parent class, this looks like the best solution.
If it is not possible to change the library, it is especially not possible to mark fields in the library class with Ignore
, so one can't fix the problem by inserting an intermediate class into the inheritance hierarchy. (I tried using a Constructor for EventDetails
with the private field as argument but this too did not work)
So in this case I think your best option would be not to try to extend from the library class (but maybe from some "surrogate parent" class) and to bridge the gap to the library class with the help of some factory class.
Thanks for the quick help. Now i won't inherit that library class. Instead i will try to use my own class everywhere and just convert my class to library compatible class only where it is required. But there might be some way to force room to only consider sub class as an entity.
– TechHelper
Nov 8 at 20:24
@TechHelper - if you find out how to do it, please write an answer to this question and accept it. (I'm sure you know you can always change the accepted answer, also for example if someone else knows how to make Room "ignore" the parent class)
– 0X0nosugar
Nov 8 at 21:01
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
This seems to be mainly a naming problem: after changing the field name from mDay to just day and renaming the setter/ getter accordingly, everything compiles.
It looks like the method names which Android Studio generates from mDay are not the ones which Room is looking for.
Since you are in a position to use different field names in the parent class, this looks like the best solution.
If it is not possible to change the library, it is especially not possible to mark fields in the library class with Ignore
, so one can't fix the problem by inserting an intermediate class into the inheritance hierarchy. (I tried using a Constructor for EventDetails
with the private field as argument but this too did not work)
So in this case I think your best option would be not to try to extend from the library class (but maybe from some "surrogate parent" class) and to bridge the gap to the library class with the help of some factory class.
Thanks for the quick help. Now i won't inherit that library class. Instead i will try to use my own class everywhere and just convert my class to library compatible class only where it is required. But there might be some way to force room to only consider sub class as an entity.
– TechHelper
Nov 8 at 20:24
@TechHelper - if you find out how to do it, please write an answer to this question and accept it. (I'm sure you know you can always change the accepted answer, also for example if someone else knows how to make Room "ignore" the parent class)
– 0X0nosugar
Nov 8 at 21:01
add a comment |
up vote
1
down vote
accepted
This seems to be mainly a naming problem: after changing the field name from mDay to just day and renaming the setter/ getter accordingly, everything compiles.
It looks like the method names which Android Studio generates from mDay are not the ones which Room is looking for.
Since you are in a position to use different field names in the parent class, this looks like the best solution.
If it is not possible to change the library, it is especially not possible to mark fields in the library class with Ignore
, so one can't fix the problem by inserting an intermediate class into the inheritance hierarchy. (I tried using a Constructor for EventDetails
with the private field as argument but this too did not work)
So in this case I think your best option would be not to try to extend from the library class (but maybe from some "surrogate parent" class) and to bridge the gap to the library class with the help of some factory class.
Thanks for the quick help. Now i won't inherit that library class. Instead i will try to use my own class everywhere and just convert my class to library compatible class only where it is required. But there might be some way to force room to only consider sub class as an entity.
– TechHelper
Nov 8 at 20:24
@TechHelper - if you find out how to do it, please write an answer to this question and accept it. (I'm sure you know you can always change the accepted answer, also for example if someone else knows how to make Room "ignore" the parent class)
– 0X0nosugar
Nov 8 at 21:01
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
This seems to be mainly a naming problem: after changing the field name from mDay to just day and renaming the setter/ getter accordingly, everything compiles.
It looks like the method names which Android Studio generates from mDay are not the ones which Room is looking for.
Since you are in a position to use different field names in the parent class, this looks like the best solution.
If it is not possible to change the library, it is especially not possible to mark fields in the library class with Ignore
, so one can't fix the problem by inserting an intermediate class into the inheritance hierarchy. (I tried using a Constructor for EventDetails
with the private field as argument but this too did not work)
So in this case I think your best option would be not to try to extend from the library class (but maybe from some "surrogate parent" class) and to bridge the gap to the library class with the help of some factory class.
This seems to be mainly a naming problem: after changing the field name from mDay to just day and renaming the setter/ getter accordingly, everything compiles.
It looks like the method names which Android Studio generates from mDay are not the ones which Room is looking for.
Since you are in a position to use different field names in the parent class, this looks like the best solution.
If it is not possible to change the library, it is especially not possible to mark fields in the library class with Ignore
, so one can't fix the problem by inserting an intermediate class into the inheritance hierarchy. (I tried using a Constructor for EventDetails
with the private field as argument but this too did not work)
So in this case I think your best option would be not to try to extend from the library class (but maybe from some "surrogate parent" class) and to bridge the gap to the library class with the help of some factory class.
answered Nov 8 at 19:42
0X0nosugar
6,60631742
6,60631742
Thanks for the quick help. Now i won't inherit that library class. Instead i will try to use my own class everywhere and just convert my class to library compatible class only where it is required. But there might be some way to force room to only consider sub class as an entity.
– TechHelper
Nov 8 at 20:24
@TechHelper - if you find out how to do it, please write an answer to this question and accept it. (I'm sure you know you can always change the accepted answer, also for example if someone else knows how to make Room "ignore" the parent class)
– 0X0nosugar
Nov 8 at 21:01
add a comment |
Thanks for the quick help. Now i won't inherit that library class. Instead i will try to use my own class everywhere and just convert my class to library compatible class only where it is required. But there might be some way to force room to only consider sub class as an entity.
– TechHelper
Nov 8 at 20:24
@TechHelper - if you find out how to do it, please write an answer to this question and accept it. (I'm sure you know you can always change the accepted answer, also for example if someone else knows how to make Room "ignore" the parent class)
– 0X0nosugar
Nov 8 at 21:01
Thanks for the quick help. Now i won't inherit that library class. Instead i will try to use my own class everywhere and just convert my class to library compatible class only where it is required. But there might be some way to force room to only consider sub class as an entity.
– TechHelper
Nov 8 at 20:24
Thanks for the quick help. Now i won't inherit that library class. Instead i will try to use my own class everywhere and just convert my class to library compatible class only where it is required. But there might be some way to force room to only consider sub class as an entity.
– TechHelper
Nov 8 at 20:24
@TechHelper - if you find out how to do it, please write an answer to this question and accept it. (I'm sure you know you can always change the accepted answer, also for example if someone else knows how to make Room "ignore" the parent class)
– 0X0nosugar
Nov 8 at 21:01
@TechHelper - if you find out how to do it, please write an answer to this question and accept it. (I'm sure you know you can always change the accepted answer, also for example if someone else knows how to make Room "ignore" the parent class)
– 0X0nosugar
Nov 8 at 21:01
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53213482%2fhow-to-use-entity-class-which-extends-another-class-in-a-room%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Just extending from another class seems to be no problem for an Entity in my sample app. Please provide more details
– 0X0nosugar
Nov 8 at 18:14
Hi @0x0nosugar, super class has few fields which are private. Because of these, it is throwing an error.
– TechHelper
Nov 8 at 18:23
I added a private field with getter and setter to the super class - still no problem. Do the private fields in your super class have getters (and setters) which are named correctly?
– 0X0nosugar
Nov 8 at 18:28
Yes the names are proper as i have generated them using alt+insert shortcut. Can we force room to only consider subclass which is entity and not it's super class?
– TechHelper
Nov 8 at 18:30
Maybe the problem is that your super class is in a library not in the module itself? (Sorry but I can't verify that immediately.) What happens if you first create a class in the module which extends from "the super class" and then you let your entity extend from the middle class?
– 0X0nosugar
Nov 8 at 18:34