My situation: I have one DB table for Object and a separate DB table for Object's extra info. Object is identified by autogenerated serial ID, Object's extra info has the same value as it's PK, so there is one-to-one relation.
I mapped Object's extra info within the instance of Object. It works fine for reading, but I am struggling while trying to save new record into DB.
Object side reference looks like this:
@JoinColumn(name = "object_id", referencedColumnName = "object_id")
@OneToOne(cascade = CascadeType.ALL)
private ObjectExtraInfo detail;
PK column in Object's extra info:
@Id
@NotNull
@Column(name = "object_id")
private Integer objectId;
I am using EntityManger.merge()
, because there are couple of other DB tables referenced, EntityManager.persist()
would be forcing me to deal with propper identification of DB entities.
It is no problem to achieve new Object record being saved, but the best I can do with attached Object's extra info was that it appears in DB with zero as PK. The auto-generated value of the new Object record is not distributed into Object's extra info. And it is impossible to change it afterwards while it is PK.
I am pretty sure my JPA annotations are messed up, especially on Object's extra info side. But so far I was unable to figure out the right approach.