简体   繁体   中英

Primary key not found in OneToOne relationship with Hibernate

I'm using Hibernate 4.2.6, JSF2 (Mojarra 2.1.13) and Tomcat 7.0.34 to develop my web application. I'm trying to define a OneToOne relationship between two classes using Hibernate annotations. I think I'm doing something wrong because I'm getting an ORA-02291 Exception: integrity constraint (PER_VPT_F01) violated - parent key not found . I've spent almost 8 hours of my time trying to find where the bug is without any success. Please, could someone give a look to this code and tell me if I am making any mistake (four eyes can see more than two).

First my parent class:

@Entity
@Table(name = "GEN_MIEMBROS")
public class Miembro implements Serializable, Comparable {

    @Id
    @SequenceGenerator(name = "seq", sequenceName = "GEN_SEQ")
    @GeneratedValue(generator = "seq")
    @Column(name = "ID_MIEMBRO")
    private long idMiembro;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "CODIGO")
    private Lista lista;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "detail")
    private Vpt vpt;

    // more fields definitions 
    // Constructors and getters & setters
}

Now, the child class:

@Entity
@Table(name = "PER_VPT")
public class Vpt implements Serializable {

    @Id
    @Column(name = "ID_VPT")
    @GenericGenerator(name = "generator", strategy = "foreign", parameters =    @Parameter(name = "property", value = "detail"))
    @GeneratedValue(generator = "generator")
    private long idVpt;

    @OneToOne
    @PrimaryKeyJoinColumn
    private Miembro detail;   

    // more fields definitions 
    // Constructors and getters & setters
}

I must say that the parent row already exists when I try to insert the new child. In fact, I get the ID for the child from the parent table. The thing is that I get an ORA-02291 Exception just when I try to commit the insert operation. As you can see in the image below, the saveOrUpdate() operation sets the value of the idVpt field previously.

在此处输入图片说明

One more thing. The constraint that throws the exception looks like this:

CONSTRAINT PER_VPT_F01 
FOREIGN KEY (ID_VPT) 
REFERENCES GEN_MIEMBROS (ID_MIEMBRO) ON DELETE CASCADE

Thanks in advance.

Marking comment as answer since it resolved OP's issue

Disable all triggers associated with the table. This should work fine. Usually it happens with triggers, where it tries another insert/update

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM