Well, I gave that a shot, but it didn't seem to work...
Not sure where to go from here, so I thought I'd post my code. As you'll see, the hbm docs below were created by the Hibernate Tools. I reverse engineered the database to create the hbm docs and the POJO's - pretty damn sweet! - but maybe that's part of my problem - not sure...
Here's the Parent .hbm doc:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-cascade="all">
<!--
Auto-generated mapping file from
the hibernate.org cfg2hbm engine
-->
<class name="com.test.domain.Items" table="items" catalog="devschema">
<id name="itemId" type="integer">
<column name="item_id" />
<generator class="select" />
</id>
<many-to-one name="users" class="com.test.domain.Users">
<column name="username" length="45" not-null="true" />
</many-to-one>
<property name="title" type="string">
<column name="title" length="60" not-null="true" />
</property>
<property name="description" type="string">
<column name="description" length="65535" />
</property>
<set name="itemsPaymentTypeses" inverse="true">
<key>
<column name="item_id" not-null="true" />
</key>
<one-to-many class="com.hypobid.domain.ItemsPaymentTypes" />
</set>
</class>
</hibernate-mapping>
And the Child .hbm doc:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--
Auto-generated mapping file from
the hibernate.org cfg2hbm engine
-->
<class name="com.test.domain.ItemsPaymentTypes" table="items_payment_types" catalog="devschema">
<composite-id name="id" class="com.test.domain.ItemsPaymentTypesId">
<key-property name="itemId" type="integer">
<column name="item_id" />
</key-property>
<key-property name="paymentTypeId" type="integer">
<column name="payment_type_id" />
</key-property>
</composite-id>
<many-to-one name="items" class="com.test.domain.Items" update="false" insert="false">
<column name="item_id" not-null="true" />
</many-to-one>
<many-to-one name="paymentTypes" class="com.test.domain.PaymentTypes" update="false" insert="false">
<column name="payment_type_id" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
And the PaymentTypes .hbm doc:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--
Auto-generated mapping file from
the hibernate.org cfg2hbm engine
-->
<class name="com.test.domain.PaymentTypes" table="payment_types" catalog="devschema">
<id name="paymentTypeId" type="integer">
<column name="payment_type_id" />
<generator class="assigned" />
</id>
<property name="paymentTypeName" type="string">
<column name="payment_type_name" length="45" not-null="true" />
</property>
<set name="itemsPaymentTypeses" inverse="true">
<key>
<column name="payment_type_id" not-null="true" />
</key>
<one-to-many class="com.test.domain.ItemsPaymentTypes" />
</set>
</class>
</hibernate-mapping>
And the code to create the Object Graph in my Controller:
Code:
Items item = (Items) command;
UserSession userSession = (UserSession)request.getSession().getAttribute("userSession");
item.setUsers(userSession.getUser());
Set mySet = new HashSet();
String[] paymentTypes = request.getParameterValues("itemsPaymentTypes");
for (int i = 0; i < paymentTypes.length; i++) {
ItemsPaymentTypesId itemsPaymentTypesId = new ItemsPaymentTypesId();
itemsPaymentTypesId.setPaymentTypeId(new Integer(paymentTypes[i]));
ItemsPaymentTypes itemsPaymentTypes = new ItemsPaymentTypes(itemsPaymentTypesId);
itemsPaymentTypes.setItems(item);
mySet.add(itemsPaymentTypes);
}
item.setItemsPaymentTypeses(mySet);
itemService.saveOrUpdateItem(item);
And part of the stack trace:
Code:
005-10-05 21:21:35,859 DEBUG [org.hibernate.util.JDBCExceptionReporter] - Could not execute JDBC batch update [insert into devschema.items_payment_types (item_id, payment_type_id) values (?, ?)]
java.sql.BatchUpdateException: Column 'item_id' cannot be null
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:642)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:294)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:193)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
So it appears as if Hibernate does not automatically set the Parent's Primary Key (i.e., item_id) to the Child record's composite Primary Key...
Any help is greatly, greatly appreciated, as this one has had me stumped!
Thanks a lot,
Matt