简体   繁体   中英

hibernate & integrity contraint violated

An app I'm working on is using Oracle and Hibernate as ORM. When I'm trying to insert PartyUserObject I keep getting following errors:

could not insert: [Person]; SQL [insert into PARTY (............)]; constraint [PARTY_FK1]
java.sql.SQLIntegrityConstraintViolationException: ORA-02291: integrity constraint (PARTY_FK1) violated - parent key not found

The message seems to be obvious, but I can't configure Hibernate to get it working. Relevant entities' relations and hbm.xml files are as follows:

PartyUser.java

private Party ap;

Party.java

private Address address;
private Set<PartyUser> partyUsers;

PartyUser.hbm.xml

<many-to-one
            name="ap"
            column="AP_ID"
            class="Party"
            not-null="true"
            lazy="false"
            cascade="save-update,evict">
</many-to-one>

Party.hbm.xml

<set name="partyUsers" inverse="true" fetch="subselect">
    <key column="AP_ID"/>
    <one-to-many class="PartyUser"/>
</set>

PARTY_FK1 refers to address FK in the party table. As I said before save is called on the DAO for a newly instantiated PartyUser object (associated Party and Address objects are new as well). All ids are generated on the java side. As you probably noticed both entities has cascade turned on.

Any ideas what's wrong?

I am not sure as to why this happens, but I see this error all the time. What I have been doing is at the beginning, when I add all of my data, first I turn off constraints with: SET DATABASE REFERENTIAL INTEGRITY FALSE and then at the end I just turn it back on SET DATABASE REFERENTIAL INTEGRITY TRUE . Not so much a fix, as it is a hack, but it works for us so I thought I would share the knowledge.

FYI, it turned out that hibernate was configured to use db sequence generation, but ids in turn were preassigned in the java code. fmdl

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