Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primary key from parent class using Room?

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.

like image 607
Shoaib Anwar Avatar asked Oct 29 '25 01:10

Shoaib Anwar


1 Answers

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.

like image 165
CommonsWare Avatar answered Oct 31 '25 16:10

CommonsWare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!