question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Field loaded from many-to-many relation disappears once the new entity object leaves the transaction

See original GitHub issue

I have the current many-to-many-relation between employees and their roles (keys and indexes are handled externally by the migration tool):


object RoleTable: UUIDTable("role") {
  val role = text("role_name")
}

class RoleEntity(id: EntityID<UUID>) : UUIDEntity(id)  {
  var role by RoleTable.role

  companion object : UUIDEntityClass<RoleEntity>(RoleTable)
}

object EmployeeTable : UUIDTable("employee") {
  val name = text("name")
}

class EmployeeEntity(id: EntityID<UUID>) : UUIDEntity(id) {
  var name by EmployeeTable.name
  var roles by RoleEntity via EmployeeRoleTable

  companion object : UUIDEntityClass<EmployeeEntity>(EmployeeTable)
}

object EmployeeRoleTable: Table("employeeRole") {
  val employeeId = reference("employee_id", EmployeeTable) //part of primary key
  val roleId = reference("role_id", RoleTable) //part of primary key
}

When I create a new employee, I want to also save their roles and return the stored employeeEntity object, including the roles.

  private fun newEmployeeEntity(
    name: String,
    roles: List<String>
  ): EmployeeEntity {

    transaction {

    val roleEntities = roles.map { 
      RoleEntity.new(UUID.randomUUID()) { this.role = role } 
    }

    val createdEmployee = EmployeeEntity.new(UUID.randomUUID()) {
      name = name
      roles = SizedCollection(roleEntities)
    }
    
    return createdEmployee
  }
}

If I check the value of createdEmployee.roles within the transaction, it contains an object of type LazySizedCollection. If I hit it with .toList or load(EmployeeEntity::roles), it resolves to a list of roleEntites as expected. But as soon as I leave the transaction, the value of roles in the returned entity becomes null.

How do I keep the list of roleEntities when I leave the transaction?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

0reactions
Tapaccommented, Oct 9, 2021

@SabrinaKall , I wasn’t able to reproduce it in test. Please check that commit

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity metadata for Role#users was not found - Stack Overflow
I am using NestJS with PostgreSQL and I had the same issue. The problem was by me is that I forgot to import...
Read more >
Updating many to many relationships in Entity Framework Core
I wrote an article called Updating many to many relationships in entity framework back on 2014 which is still proving to be popular...
Read more >
5 Common Hibernate Mistakes That Cause Dozens of ...
JPA and Hibernate get often criticized for executing much more queries than expected. That's often caused by a few mistakes that you can...
Read more >
Changing Foreign Keys and Navigations - EF Core
How to change relationships between entities by manipulating foreign keys and navigations.
Read more >
Create a many-to-many relationship - ServiceNow Docs
Many-to-many relationships allow a list to point to a list of entries, rather than to single field.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found