I am using Android Architecture Components. Hence using Room
'ORM'. I have a class EQPreset
that has a member String presetName
. This class has a child class called UserDefinedEQPreset
and it contains an int[] arr
. I have declared the child class EQPreset
and entity using @Entity
annotation, since only this subtype I want to store in db. Now I want to use parent class's (EQPreset
) member String presetName
to be used as primary key
. How to declare a member of parent class as primary key
while using Room
. I know @Primarykey
annotation is used to declare the primary key. But how to use parent class's member as primary key
.
Asked
Active
Viewed 2,448 times
3

Shoaib Anwar
- 1,555
- 12
- 26
2 Answers
6
Put the @PrimaryKey
annotation on the parent class' field. Done.
For example, in this sample app, I have an abstract class Plan
with @PrimaryKey
public final String id
. All subclasses, such as Trip
, inherit that @PrimaryKey
definition.
Not everything inherits properly (e.g., @TypeConverters
works on fields but not on classes), but @PrimaryKey
seems to.

CommonsWare
- 986,068
- 189
- 2,389
- 2,491
-
Commonsware can i do the other way round ? Lets say if i have a table Feed(@Entity) & using @Embedded (on Post) i add more fields into Feed table from my class Post but i want to specify primary key from class Post – Gautam Oct 09 '17 at 09:13
-
@Gautam: I suggest that you ask a separate Stack Overflow question, as I do not completely understand what you are trying to do. Sorry! – CommonsWare Oct 09 '17 at 11:12
0
@Entity(primaryKeys = ["presetName"])
class UserDefinedEQPreset extends EQPreset{
...
}
I think this is what you're looking for. Use the primaryKeys
attribute on the Entity
annotation to specify the primary key. You can also use this to specify composite primary keys.

Markymark
- 2,804
- 1
- 32
- 37

Edwin Nyawoli
- 836
- 8
- 20