-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: can't cascade save children with composite-id
PostPosted: Sat May 30, 2009 3:09 am 
Newbie

Joined: Sat May 30, 2009 2:47 am
Posts: 12
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.

Top
 Profile  
 
 Post subject: Re: can't cascade save children with composite-id
PostPosted: Mon Jun 01, 2009 8:55 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Quote:
<map-key type="long" column="PREFERENCE_TYPE_ID"/>
<one-to-many class="AddressPreference" />
</map>


Did you try adding cascade attribute to the mapping?

_________________
Sukirtha


Top
 Profile  
 
 Post subject: Re: can't cascade save children with composite-id
PostPosted: Mon Jun 01, 2009 2:16 pm 
Newbie

Joined: Sat May 30, 2009 2:47 am
Posts: 12
Yes, there is cascade setting for sure. It is just when I copy it into the post, I missed one line. I edit post with the exact setting in my mapping files. Sorry for confusion.

The problem is there when there is a cascade setup in one-to-many side.


Top
 Profile  
 
 Post subject: Re: can't cascade save children with composite-id
PostPosted: Mon Jun 01, 2009 6:11 pm 
Newbie

Joined: Sat May 30, 2009 2:47 am
Posts: 12
I found answer to it from this page, assuming it is correct, quote as follows:

https://hibernate.bluemars.net/117.html?cmd=prntdoc

"I have a composite key where one column holds a generated value - how can I get Hibernate to generate and assign the value?
We regard this as an extremely strange thing to want to do. If you have a generated surrogate key, why not just make it be the primary key?

However, if you must do this, you can do it by writing a CompositeUserType for the composite identifier class, and then defining a custom IdentifierGenerator that populates the generated value into the composite key class."


Top
 Profile  
 
 Post subject: Re: can't cascade save children with composite-id
PostPosted: Sun Aug 05, 2012 3:19 am 
Newbie

Joined: Sun Aug 05, 2012 3:13 am
Posts: 1
I have found a way to insert a new entity with a new joined (children) entity together by cascade.

See http://www.rndblog.com/hibernate-how-to-insert-onetomany-children-by-cascade/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.