The parent class is Address, the child class is AddressPreference. AddressPreference has a composite key, which one of them is the foreign key to Address. The one-to-many relationship from Address will cascade save-update AddressPreference. However, when I persist(saveOrUpdate), It keeps complaining about constraint voilation exception.
Here are the relevant mapping information:
--------- Address.hbm.xml -------------------
<class name="Address" table="address" <id name="addressId" type="long"> <column name="ADDRESS_ID" /> <generator class="identity" /> </id>
<map name="addressPreferences" inverse="true" cascade="save-update, persist, merge,remove, delete"> <key column="ADDRESS_ID" not-null="true" /> <map-key type="long" column="PREFERENCE_TYPE_ID"/> <one-to-many class="AddressPreference" /> </map> ... </class>
------- AddressPreference.hbm.xml ------------------- <class name="AddressPreference" table="address_preference"> <composite-id name="id" class="AddressPreferenceId"> <key-property name="addressId" column="ADDRESS_ID" /> <key-property name="preferenceTypeId" type="long"> <column name="PREFERENCE_TYPE_ID" /> </key-property> </composite-id>
<many-to-one name="address" class="Address" update="false" insert="false" fetch="join"> <column name="ADDRESS_ID" not-null="true" /> </many-to-one>
... </class>
-------------
Here is the operation. I am using hibernate JPA interface, which internally use hibernate =============================================================================== Address address = new Address();
AddressPreference pref = new AddressPreference();
AddressPreferenceId pid = new AddressPreferenceId(); pid.setPreferenceTypeId(9); //don't know the id of address yet, therefore, can't assign it to pid.
Map<Long, AddressPreference> preferences = new HashMap<Long, AddressPreference>(); preferences.put(new Long(9), pref); address.setAddressPreferences(preferences);
Tx.start();
entityManager.save(address);
tx.commit();
====================================================================== The generated query and exception I have been getting is:
Hibernate: insert into clm.wcp_address (FIRST_NAME, MIDDLE_NAME, LAST_NAME, ADDR1, ADDR2, ADDR3, CITY, STATE, ZIP, COUNTRY, CREATED_DTM, OMS_ADDRESS_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) Hibernate: insert into clm.wcp_address_preference (SOURCE_ID, CURRENT_STATUS, STATUS_CHANGE_DTM, CREATED_BY, CREATED_DTM, MODIFIED_BY, MODIFIED_DTM, INACTIVE_DATE, CUSTOMER_ID, ADDRESS_HISTORY_ID, ADDRESS_ID, PREFERENCE_TYPE_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 4317 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 1452, SQLState: 23000 4317 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Cannot add or update a child row: a foreign key constraint fails (`clm`.`wcp_address_preference`, CONSTRAINT `WCP_ADDRESS_PREFERENCE_IBFK_2` FOREIGN KEY (`ADDRESS_ID`) REFERENCES `wcp_address` (`ADDRESS_ID`) ON DELETE NO ACTION) 4317 [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
Last edited by shaoxianyang on Mon Jun 01, 2009 2:15 pm, edited 1 time in total.
|