@Entity, @Embedded and Composite Primary Keys with Room DB

Photo by Caspar Rubin on Unsplash

Data classes hold data.

And @Entity makes a table out of the data class.

The @Entity annotation indicates that the data class is to be mapped to a database.

But the address variables relate directly to Address and not to Person.

Multiple variables relate to Address which in turn relates to Person. Therefore, it does sound more competent to use an object for storing the address.

person.address.city

So what if all these variables needed to be directly accessed from Person as person.city?

@Embedded makes this happen.

In a data class, this annotation indicates that the instance of the class being embedded is stored as an intrinsic part of the class where the annotation is being used.

@Embedded can be used to ‘snapshot’ the state of a related class inside the parent class.

Meaning, the structuring of the data isn’t actually changed. The fields from the embedded class are merged with the parent class. These fields can be used as if they were declared in the parent class as required.

person.city

Composite Primary Key

In JPA (Java Persistence API), we can add multiple Primary Keys by creating an @Embeddable class. Using it in the @Entity class as @EmbeddedId would be sufficient for a composite Primary Key.

An @Embedded field cannot contain Primary Key.

If sub fields of an embedded field has PrimaryKey annotation, they will not be considered as primary keys in the owner Entity.

If multiple primary keys are to be defined, then a list of Primary Key column names may be defined as:

String[] primaryKeys ()
@PrimaryKey(autoGenerate = true)    
val id: Long

There is one more option.

If PrimaryKey annotation is used on an Embedded field, all columns inherited from that embedded field becomes the composite primary key (including its grand children fields).

--

--

Android/RN dev | WWCode Tokyo Lead | Writer | Hypocrite with an appreciation of the beautiful horror of femininity | she/her.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kinnera Priya Putti

Kinnera Priya Putti

259 Followers

Android/RN dev | WWCode Tokyo Lead | Writer | Hypocrite with an appreciation of the beautiful horror of femininity | she/her.