I'm having problems with generating primary keys with one-to-one relations that use shared primary key.
Here's code:
@Entity
@Table(name = "osoba")
public class Osoba implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "osoba_id")
private Integer osobaId;
@PrimaryKeyJoinColumn
@OneToOne(cascade = CascadeType.PERSIST)
public Pracownik pracownik;
...
and second class:
@Entity
@Table(name = "pracownik")
public class Pracownik
{
@OneToOne
@JoinColumn(name = "osoba_id")
@MapsId("osobaId")
private Osoba osoba;
@Id
@Column(name = "osoba_id")
private Integer osobaId;
...
I've been similar issues and I thought that i've done everything correctly but i still get
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): entity.Pracownik
when trying to persist Pracownik objects.
You need to follow example from
@MapsId
documentation (in your case, with@Id
instead of@EmbeddedId
):Inverse side of
@OneToOne
relationship should be mapped withmappedBy
, as usually: