Issue
I have defined entities as the following:
@Entity
data class Environment(
@EmbeddedId
var key: EnvStageId? = null,
var value: String = ""
)
@Embeddable
class EnvStageId(
@ManyToOne
var stage: Stage? = null,
var key: String = ""
) : Serializable
@Entity
data class Stage(
@field:Id
var env: String = "",
var description: String = ""
)
I used Hibernate and it generates the tables as expected. The question is, did I define ManyToOne
correctly or did I miss something? Regarding this this tutorial, I have to define ManyToOne
on one side and OneToMany
on other side.
Solution
You don't have to define the other side of the relationship. You only have to do it if you want to have access to the relations from both side.
If you want to do that you'll have to do something like this. I'm not familiar with @Embeddable
, but I imagine that it should be similar to this:
@Entity
data class Environment(
@OneToMany(mappedBy = "environment", cascade = CascadeType.ALL)
var stages: List<Stage>
)
@Entity
data class Stage(
@ManyToOne
var environment: Environment
)
Answered By - Adam Arold