TouK/krush

Optional OneToOne is unsupported

Closed this issue · 1 comments

There's no strict documentation, so I'm not sure if I'm right, but it looks like Krush can not support nullable (optional) values in OneToOne relations...
The following has also the same behavior when the relation is bidirectional instead of unidirectional, so there is no difference.

Imagine the following usecase: I have an entity Box and an entity Item. I want a Box to contain either an Item or nothing, so I write

@Entity
data class Item(
  @Id @GeneratedValue val id: Long? = null,
  val nonNullable: NotNull // any non-nullable type
)

@Entity
data class Box(
  @Id @GeneratedValue val id: Long? = null,
  @OneToOne(optional = true) @JoinColumn(name = "box_id") val item: Item? = null,
  // some other data maybe
)

What I expect from this mapping is, obviously, to have

  1. a BoxTable with val item: Column<Long?> = long("whatever").references(Item.id).nullable()
  2. a ResultRow.toBox(), which, as I can assume, will return a Box with item = null when there is no corresponding Item.

There are no issues with the first point, but ResultRow.toBox() looks like

fun ResultRow.toBox(): Box = Box(
  id = this[BoxTable.id],
  item = this.toItem(),
  ...
)

where this.toItem() returns an Item instead of Item? resulting in NullPointerException when trying to construct an Item from null row fields.

There is a workaround for this, making all Item's fields nullable and just checking if Item.id is null after calling .toBox(), but it seems a bit ugly, so I just wanted to ask if this usecase is going to be supported or it's better to use some other relation/entity structure.

@mproch Hello, sorry for tagging you, but there was no response for almost a month now, so I thought you didn't notice.. :)