Field loaded from many-to-many relation disappears once the new entity object leaves the transaction
See original GitHub issueI 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:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
https://github.com/JetBrains/Exposed/issues/281#issuecomment-925968534
@SabrinaKall , I wasn’t able to reproduce it in test. Please check that commit