I am having problems with a fairly straight forward one-one mapping. I have a Vendor (parent) that has a one-to-one mapping with Contact (chils).
The Contact table has a FK constraint on the Vendor table.
I am getting a FK constrained violation primarily because HB is trying to save Contact instead of saving Vendor first and then Contact.
Please advise.
Below are the Vendor and Contact mappings respectively:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.demo.hb.Vendor" table="HB_VENDOR">
<id name="vendorId" column="VENDORID">
<generator class="foreign">
<param name="property">vendorContact</param>
</generator>
</id>
<property name="vendorName" column="VENDORNAME"/>
<one-to-one name="vendorContact" class="com.demo.hb.Contact" cascade="all" constrained="true"/>
</class>
</hibernate-mapping>
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.demo.hb.Contact" table="HB_VENDOR_CONTACT">
<id name="contactId" column="VENDORID" unsaved-value="any">
<generator class="sequence">
<param name="sequence">seq_vendorid</param>
</generator>
</id>
<property name="contactCity" column="CITY"/>
<property name="contactCountry" column="COUNTRY"/>
<property name="contactFirstName" column="CONTACTFIRSTNAME"/>
<property name="contactLastName" column="CONTACTLASTNAME"/>
<property name="contactLine1" column="ADDRESSLINE1"/>
<property name="contactLine2" column="ADDRESSLINE2"/>
<property name="contactState" column="STATE"/>
<property name="contactTitle" column="CONTACTTITLE"/>
<property name="contactZip" column="ZIP"/>
<one-to-one name="contactVendor" class="com.demo.hb.Vendor" cascade="all" constrained="true"/>
</class>
</hibernate-mapping>
Also here is the stack trace with logging turned on:
Code:
17:42:55,304 DEBUG SessionFactoryImpl:281 - instantiated session factory
17:42:55,314 DEBUG SessionImpl:413 - opened session
17:42:55,324 DEBUG BatcherImpl:166 - about to open: 0 open PreparedStatements, 0 open ResultSets
17:42:55,324 DEBUG DriverManagerConnectionProvider:77 - total checked-out connections: 0
17:42:55,324 DEBUG DriverManagerConnectionProvider:83 - using pooled JDBC connection, pool size: 0
17:42:55,324 DEBUG SessionFactoryImpl:526 - prepared statement get: select seq_vendorid.nextval from dual
17:42:55,334 DEBUG SessionFactoryImpl:536 - preparing statement
17:42:55,545 DEBUG SequenceGenerator:70 - Sequence identifier generated: 100020
17:42:55,545 DEBUG BatcherImpl:173 - done closing: 0 open PreparedStatements, 0 open ResultSets
17:42:55,545 DEBUG SessionFactoryImpl:554 - closing statement
17:42:55,555 DEBUG SessionImpl:656 - saving [com.demo.hb.Contact#100020]
17:42:55,555 DEBUG Cascades:336 - processing cascades for: com.demo.hb.Contact
17:42:55,565 DEBUG Cascades:344 - done processing cascades for: com.demo.hb.Contact
17:42:55,565 DEBUG Cascades:336 - processing cascades for: com.demo.hb.Contact
17:42:55,565 DEBUG Cascades:344 - done processing cascades for: com.demo.hb.Contact
17:42:55,575 DEBUG SessionImpl:656 - saving [com.demo.hb.Vendor#100020]
17:42:55,575 DEBUG Cascades:336 - processing cascades for: com.demo.hb.Vendor
17:42:55,575 DEBUG Cascades:87 - cascading to saveOrUpdate()
17:42:55,575 DEBUG SessionImpl:1183 - saveOrUpdate() persistent instance
17:42:55,575 DEBUG Cascades:344 - done processing cascades for: com.demo.hb.Vendor
17:42:55,575 DEBUG Cascades:336 - processing cascades for: com.demo.hb.Vendor
17:42:55,575 DEBUG Cascades:344 - done processing cascades for: com.demo.hb.Vendor
17:42:55,575 DEBUG SessionImpl:2011 - flushing session
17:42:55,575 DEBUG Cascades:336 - processing cascades for: com.demo.hb.Contact
17:42:55,585 DEBUG Cascades:344 - done processing cascades for: com.demo.hb.Contact
17:42:55,585 DEBUG Cascades:336 - processing cascades for: com.demo.hb.Vendor
17:42:55,585 DEBUG Cascades:87 - cascading to saveOrUpdate()
17:42:55,585 DEBUG SessionImpl:1183 - saveOrUpdate() persistent instance
17:42:55,585 DEBUG Cascades:344 - done processing cascades for: com.demo.hb.Vendor
17:42:55,585 DEBUG SessionImpl:2113 - Flushing entities and processing referenced collections
17:42:55,585 DEBUG SessionImpl:2397 - Processing unreferenced collections
17:42:55,595 DEBUG SessionImpl:2408 - Scheduling collection removes/(re)creates/updates
17:42:55,595 DEBUG SessionImpl:2023 - Flushed: 2 insertions, 0 updates, 0 deletions to 2 objects
17:42:55,595 DEBUG SessionImpl:2028 - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
17:42:55,595 DEBUG SessionImpl:2058 - executing flush
17:42:55,595 DEBUG EntityPersister:464 - Inserting entity: com.demo.hb.Contact#100020
17:42:55,595 DEBUG BatcherImpl:166 - about to open: 0 open PreparedStatements, 0 open ResultSets
17:42:55,595 DEBUG SessionFactoryImpl:526 - prepared statement get: insert into HB_VENDOR_CONTACT (CITY, COUNTRY, CONTACTFIRSTNAME, CONTACTLASTNAME, ADDRESSLINE1, ADDRESSLINE2, STATE, CONTACTTITLE, ZIP, VENDORID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
17:42:55,595 DEBUG SessionFactoryImpl:536 - preparing statement
17:42:55,595 DEBUG EntityPersister:366 - Dehydrating entity: com.demo.hb.Contact#100020
17:42:55,605 DEBUG BatcherImpl:24 - Adding to batch
17:42:55,605 DEBUG EntityPersister:464 - Inserting entity: com.demo.hb.Vendor#100020
17:42:55,605 DEBUG BatcherImpl:46 - Executing batch size: 1
17:42:55,615 DEBUG JDBCExceptionReporter:36 - SQL Exception
java.sql.BatchUpdateException: ORA-02291: integrity constraint (EP10.HB_VENDOR_CONTACT_FK) violated - parent key not found
at oracle.jdbc.dbaccess.DBError.throwBatchUpdateException(DBError.java:459)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:4133)
at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:50)
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:105)