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

}









share|improve this question
























  • 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















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

}









share|improve this question
























  • 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













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

}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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.






share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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.






share|improve this answer





















  • 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















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.






share|improve this answer





















  • 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













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)