Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Let room [email protected] of objects with @Embedded and @Relation annotations

Posted on November 11, 2022 By

Solution 1 :

I made it work on my own.
As I thought the main problem was the autogenerated int key of Licence used as foreign key. A “static” string and splitting the two objects in the @Insert method did the trick.
I’ll leave it here, hope it helps someone:

Licence:

@Entity(tableName = "licence")
data class Licence(
@PrimaryKey
@ColumnInfo(name = "function_id")
var functionId: String,

@ColumnInfo(name = "expiration_date")
var expirationDate: Date,
//more attributes )

LicenceConfigurations:

@Entity(
foreignKeys = [
    ForeignKey(
        entity = Licence::class,
        parentColumns = ["function_id"],
        childColumns = ["licence_reference"],
        onDelete = ForeignKey.CASCADE,
        onUpdate = ForeignKey.CASCADE
    )],tableName = "licence_configurations")
data class LicenceConfig(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "licence_config_id")
var licenceConfigId: Int,

@ColumnInfo(name = "licence_reference")
var licenceId: Int
//more attributes )

LicenceWithConfigurations:

data class LicenceWithConfigurations(
@Embedded
val licence: Licence,
@Relation(
parentColumn = "licence_id",
entityColumn = "licence_reference",
entity = LicenceConfig::class)
val licenceConfig: List<LicenceConfig>?)

LicenceDao:

 @Transaction
 @Insert(onConflict = OnConflictStrategy.REPLACE)
 suspend fun insertLicenceWithConfigurations(licence:Licence, licenceConfigurations: List<LicenceConfig>?)

@Transaction
@Query("SELECT * FROM licence")
suspend fun getAllLicencesWithConfigurations(): List<LicenceWithConfigurations>

Problem :

I’ve read a ton of q/a and articles about room and foreign keys and I’m almost convinced that I can’t actually achieve what I’m trying to do. Also most of the examples/tutorials explain only select query.

So, there’s a classic one to many relationship and I would like to define an object with @Embedded and @Relation to obtain an “one shot” insert method for an object containing a list of objects.
To be more clear:

Licence:

@Entity(tableName = "licence")
data class Licence(
  @PrimaryKey(autoGenerate = true)
  @ColumnInfo(name = "licence_id")
  var licenceId: Int,
  @ColumnInfo(name = "expiration_date")
  var expirationDate: Date
  //other attributes  )

LicenceConfigurations:

@Entity(
foreignKeys = [
    ForeignKey(
        entity = Licence::class,
        parentColumns = ["licence_id"],
        childColumns = ["licence_reference"],
        onDelete = ForeignKey.CASCADE,
        onUpdate = ForeignKey.CASCADE
    )],
tableName = "licence_configurations")
data class LicenceConfig(
  @PrimaryKey(autoGenerate = true)
  @ColumnInfo(name = "licence_config_id")
  var licenceConfigId: Int,
  @ColumnInfo(name = "licence_reference")
 var licenceId: Int
//other attributes  )

LicenceWithConfigurations

data class LicenceWithConfigurations(
@Embedded
val licence: Licence,
@Relation(
    parentColumn = "licence_id",
    entityColumn = "licence_reference",
    entity = LicenceConfig::class)
val licenceConfig: List<LicenceConfig>?)

What I’m trying to do is to avoid @Transaction and perform the first insert for Licence, retrieve the id, set the id for each LicenceConfiguration and perform another insert.
I would like instead to have a method like that in the DAO:

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertLicenceWithConfigurations(licenceWithConfigurations: LicenceWithConfigurations)

Right now the error is “FOREIGN KEY constraint failed (code 787 SQLITE_CONSTRAINT_FOREIGNKEY)”, so I have specified the @Index for LicenceConfig class then tried to set the foreign key on another field (because I have read that with autogenerated id as foreign keys it will not works) but is still not working. Can I do that or should I go back with @Transaction and handle the insertions manually?

READ  [FIXED] java - Parse json and get inner object
Powered by Inline Related Posts
Android Tags:android, android-room, kotlin

Post navigation

Previous Post: [FIXED] android – how to change notification background color in nougat?
Next Post: [FIXED] android – How to make width ratio equal in XML GridView

Related Posts

[FIXED] memory – Writing data in native heap to disk in Android Android
[FIXED] android – problem in open another activity from a custom adapter in java Android
[FIXED] kotlin – How can I share info among @Composable function in Android Studio? Android
[FIXED] java – How to resolve so file mapping in eclipse project aosp android Android
[FIXED] android – Changing the Searched Location for google-services.json using complex flavour tree Android
[FIXED] java – Android Studio Linear Layout positioning not working? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme