Shared primary key generation. Hibernate

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.

Answers


You need to follow example from @MapsId documentation (in your case, with @Id instead of @EmbeddedId):

@Entity
@Table(name = "pracownik")
public class Pracownik {
    @Id 
    @Column(name = "oboba_id")
    private Integer id;

    @OneToOne
    @MapsId
    private Osoba osoba;

    ...
}

Inverse side of @OneToOne relationship should be mapped with mappedBy, as usually:

@Entity
@Table(name = "osoba")
public class Osoba implements Serializable {
    ...
    @OneToOne(mappedBy = "osoba", cascade = CascadeType.PERSIST)
    public Pracownik pracownik;
    ...
}

This old question but it works for me. Mayby it will help someone.

SQL Script (Oracle)

DROP TABLE HIBERNATE.PRACOWNIK;
DROP TABLE HIBERNATE.OSOBA;
DROP SEQUENCE HIBERNATE.OSOBA_SEQ;

CREATE TABLE HIBERNATE.OSOBA (
osoba_id                                NUMBER(15),
CONSTRAINT OSOBA_PK PRIMARY KEY (osoba_id)
);

CREATE TABLE HIBERNATE.PRACOWNIK (
    pracownik_id                                NUMBER(15),
    CONSTRAINT PRACOWNIK_PK PRIMARY KEY (pracownik_id),
    CONSTRAINT PRACOWNIK_FK FOREIGN KEY (pracownik_id) REFERENCES OSOBA(osoba_id)
);

CREATE SEQUENCE HIBERNATE.OSOBA_SEQ START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE;

Osoba.java

@Entity
@Table(name = "osoba")
public @Data class Osoba {

    @Id
    @Column(name = "osoba_id")
    @GeneratedValue(generator="osoba-generator", strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name="osoba-generator", allocationSize = 1, sequenceName = "OSOBA_SEQ")
    private Long osobaId;

    @OneToOne(fetch=EAGER, mappedBy="osoba", cascade=ALL)
    private Pracownik pracownik;
}

Pracownik.java

@Entity
@Table(name="pracownik")
public @Data class Pracownik {

    @Id
    @Column(name = "pracownik_id")
    @GeneratedValue(generator="pracownik-generator")
    @GenericGenerator(name="pracownik-generator", strategy="foreign", parameters=
        @Parameter(name = "property", value = "osoba")
    )
    private Long pracownikId;

    @OneToOne(fetch=FetchType.EAGER)
    @PrimaryKeyJoinColumn
    private Osoba osoba;

}

@Data is Lombok


Need Your Help

Java LDAP auth with a non-cleartext password

java security authentication ldap jndi

JNDI LDAP auth requires a cleartext password to be passed (Context.SECURITY_CREDENTIALS) to almost all security mechanisms (or at least how I understand it). It looks like to be designed in a way, ...

routing subfolder on S3 to wordpress blog on EC2 microsoft server

amazon-web-services amazon-ec2 amazon-s3

I'm using Amazon S3 to host static files for my website. Now I wanted to add Wordpress blog, which would be hosted in /blog subfolder. I followed an article which explains the easiest way to install