hi,
i was recently having some trouble with a many-to-many relationship and while i fixed this problem, during testing i noticed that my transactions were not being rolled back on error. here are the mapping files i was using, notice that both ends of the many-to-many relationship are set inverse="false" resulting in two inserts into the join table.
Code:
<hibernate-mapping>
<class name="my.package.Account" table="Account">
<id name="id" column="id" type="java.lang.Long" >
<generator class="hilo"/>
</id>
<set name="accountHolders" table="AccountGroup" lazy="true"
inverse="false" cascade="save-update">
<key column="account" />
<many-to-many class="my.package.Customer"
column="customer" outer-join="auto" />
</set>
</class>
<class name="my.package.Customer" table="Customer">
<id name="id" column="id" type="java.lang.Long" >
<generator class="hilo"/>
</id>
<set name="accounts" table="AccountGroup" lazy="true"
inverse="false" cascade="save-update" >
<key column="customer" />
<many-to-many class="my.package.Account"
column="account" outer-join="auto" />
</set>
</class>
</hibernate-mapping>
and here's my test code:
Code:
Session session = null;
Transaction transaction = null;
try {
session = factory.openSession();
transaction = session.beginTransaction();
Customer customer = new Customer();
Account account = new Account();
account.addAccountHolder(customer);
customer.addAccount(account);
session.save(account);
session.save(customer);
transaction.commit();
} catch (HibernateException e) {
try {
transaction.rollback();
} catch (HibernateException e1) {}
} finally {
try {
session.close();
} catch (HibernateException e) {}
}
and here's the exception:
Code:
java.sql.BatchUpdateException: Invalid argument value, message from server: "Duplicate entry '5308417-5275649' for key 1"
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1446)
at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:54)
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:118)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2311)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2265)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2187)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at com.client.business.AccountTransactionTest.main(AccountTransactionTest.java:52)
so, after performing the test, receiving the exception and being told that the transaction has been rolled back, when i look in the database the account, customer and one of the attempted account-customer relationships have been commited. this shouldn't this happen, should it?
thanks, cam