I am using Hibernate3 in a Spring 2.x framework. Everything works well except that only the last item out of n is stored in the "many" table instead of all.
Here is the mapping :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.noy.bookstore.model.CustomerOrder"
table="customerorder">
<id name="id" type="int" column="id" unsaved-value="0">
<generator class="native" />
</id>
<property name="firstName">
<column name="firstname" sql-type="varchar(100)"
not-null="true" />
</property>
<property name="lastName">
<column name="lastname" sql-type="varchar(100)"
not-null="true" />
</property>
<property name="address">
<column name="address" sql-type="varchar(100)"
not-null="true" />
</property>
<property name="zipCode">
<column name="zipcode" sql-type="varchar(10)"
not-null="true" />
</property>
<property name="orderDate">
<column name="orderdate" sql-type="datetime"
not-null="true" />
</property>
<property name="cardNumber">
<column name="cardnumber" sql-type="varchar(20)"
not-null="true" />
</property>
<bag name="orderItems" cascade="save-update">
<key column="orderid" />
<one-to-many class="com.noy.bookstore.model.OrderItem" />
</bag>
<many-to-one column="orderStatus" name="orderStatus"
class="com.noy.bookstore.model.OrderStatus" not-null="true" />
</class>
</hibernate-mapping>
____________________________________________________________
Here is the application context:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/b ... -beans.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/bookstore" />
<property name="username" value="elan" />
<property name="password" value="elan" />
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>
com/noy/bookstore/model/Registration.hbm.xml
</value>
<value>com/noy/bookstore/model/Book.hbm.xml</value>
<value>com/noy/bookstore/model/Category.hbm.xml</value>
<value>
com/noy/bookstore/model/CustomerOrder.hbm.xml
</value>
<value>com/noy/bookstore/model/OrderItem.hbm.xml</value>
<value>
com/noy/bookstore/model/OrderStatus.hbm.xml
</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean>
<!-- Transaction manager for the Hibernate SessionFactory. -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- RegistrationDAO: Hibernate implementation -->
<bean id="registrationDAO"
class="com.noy.bookstore.dao.hibernate.RegistrationDAOHibernate">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- Registration Service -->
<bean id="registrationServiceTarget"
class="com.noy.bookstore.business.impl.RegistrationServiceImpl">
<property name="registrationDAO">
<ref local="registrationDAO" />
</property>
</bean>
<!-- Transaction declarations for Registration Service -->
<bean id="registrationService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="target">
<ref local="registrationServiceTarget" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert">PROPAGATION_REQUIRED</prop>
<prop key="update">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- bookstoreDAO: Hibernate implementation -->
<bean id="bookstoreDao"
class="com.noy.bookstore.dao.hibernate.BookstoreDaoImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- bookstore Service -->
<bean id="bookstoreServiceTarget"
class="com.noy.bookstore.business.impl.BookstoreServiceImpl">
<property name="bookstoreDao">
<ref local="bookstoreDao" />
</property>
</bean>
<!-- Transaction declarations for bookstore Service -->
<bean id="bookstoreService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="target">
<ref local="bookstoreServiceTarget" />
</property>
<property name="transactionAttributes">
<props>
<prop key="SAVE_UPDATE">PROPAGATION_REQUIRED</prop>
<prop key="SAVE_UPDATE">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
__________________________________________________________Some log infor:
flushing session
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | processing flush-time cascades
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | processing cascade ACTION_SAVE_UPDATE for: com.noy.bookstore.model.CustomerOrder
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | cascade ACTION_SAVE_UPDATE for collection: com.noy.bookstore.model.CustomerOrder.orderItems
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | cascading to saveOrUpdate: com.noy.bookstore.model.OrderItem
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | persistent instance of: com.noy.bookstore.model.OrderItem
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | ignoring persistent instance
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | object already associated with session: [com.noy.bookstore.model.OrderItem#18]
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | cascading to saveOrUpdate: com.noy.bookstore.model.OrderItem
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | persistent instance of: com.noy.bookstore.model.OrderItem
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | ignoring persistent instance
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | object already associated with session: [com.noy.bookstore.model.OrderItem#18]
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | done cascade ACTION_SAVE_UPDATE for collection: com.noy.bookstore.model.CustomerOrder.orderItems
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | done processing cascade ACTION_SAVE_UPDATE for: com.noy.bookstore.model.CustomerOrder
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | dirty checking collections
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | Flushing entities and processing referenced collections
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | Collection found: [com.noy.bookstore.model.CustomerOrder.orderItems#11], was: [<unreferenced>] (initialized)
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | Processing unreferenced collections
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | Scheduling collection removes/(re)creates/updates
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | Flushed: 0 insertions, 0 updates, 0 deletions to 3 objects
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | listing entities:
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | com.noy.bookstore.model.CustomerOrder{id=11, lastName=k, address=k, orderDate=2008-04-27 08:30:35, zipCode=k, orderItems=[com.noy.bookstore.model.OrderItem#18, com.noy.bookstore.model.OrderItem#18], firstName=k, orderStatus=com.noy.bookstore.model.OrderStatus#1, cardNumber=k}
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | com.noy.bookstore.model.OrderItem{id=18, order=null, book=com.noy.bookstore.model.Book#7, unitPrice=109.99, qty=1}
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | com.noy.bookstore.model.OrderStatus{id=1, name=DECLINED PAYMENT}
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | executing flush
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | registering flush begin
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | Inserting collection: [com.noy.bookstore.model.CustomerOrder.orderItems#11]
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | update orderitem set orderid=? where id=?
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | preparing statement
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | binding '11' to parameter: 1
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | binding '18' to parameter: 2
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | reusing prepared statement
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | update orderitem set orderid=? where id=?
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | binding '11' to parameter: 1
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | binding '18' to parameter: 2
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | done inserting collection: 2 rows inserted
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | Executing batch size: 2
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | closing statement
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | registering flush end
[Struts2WebApp] DEBUG [2008/04/27 08:30:35] | post flush