I have a problem with the following mapping. There are 2 entities (TypeOne and TypeTwo) with a many-to-many relation. So I created the mapping as followings. The problem occurs when I try to create a TypeOne object and associate it with an existing TypeTwo object in one single transaction, Hibernate throws exception but it won't happen when there's no transaction (ie autocommit = true). I couldn't figure out the problem after few days goolging. Please help!
Hibernate version: 3.2.2
Mapping documents:
<class
name="TypeOne"
table="T_TypeOne">
<id
name="id"
type="long"
unsaved-value="null">
<column
name="TypeOne_ID"
length="20" />
<generator class="identity" />
</id>
<natural-id mutable="false">
<property
name="uid"
type="string">
<column
name="UID"
length="128" />
</property>
</natural-id>
<property
name="name"
type="string">
<column
name="`NAME`"
length="20" />
</property>
</class>
<class
name="TypeTwo"
table="T_TypeTwo">
<cache usage="read-write" />
<id
name="id"
column="TypeTwo_ID"
type="long"
length="20"
unsaved-value="null">
<generator class="identity" />
</id>
<version
name="version"
unsaved-value="null">
<column
name="VERSION"
not-null="false" />
</version>
<property
name="name"
type="string"
update="false">
<column
name="`NAME`"
length="255"
not-null="true" />
</property>
</class>
<class
name="TypeOneTypeTwo"
table="T_TypeOne_TypeTwo">
<id
name="id"
type="com.my.model.DoubleLongType">
<column name="TypeTwo_ID" />
<column name="TypeOne_ID" />
<generator class="assigned" />
</id>
<version
name="version"
unsaved-value="null">
<column
name="VERSION"
not-null="false" />
</version>
<property
name="deleted"
type="boolean">
<column
name="DELETED" />
</property>
<many-to-one
name="typeTwo"
insert="false"
update="false"
lazy="proxy">
<column name="TypeTwo_ID" />
</many-to-one>
<many-to-one
name="typeOne"
insert="false"
update="false"
lazy="proxy">
<column name="TypeOne_ID" />
</many-to-one>
</class>
Code between sessionFactory.openSession() and session.close():
TypeOne to = tott.getTypeOne();
boolean isNew = to.getId() == null;
if(isNew) {
dao.saveTypeOne(to);
}
tott.setId(new DoubleLong(tott.getTypeTwo().getId(), to.getId()));
dao.saveTypeOneTypeTwo(tott);
Full stack trace of any exception that occurs:
WARN [main] JDBCExceptionReporter.logExceptions(77) | SQL Error: 0, SQLState: null
ERROR [main] JDBCExceptionReporter.logExceptions(78) | failed batch
ERROR [main] AbstractFlushingEventListener.performExecutions(301) | Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:387)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:368)
at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:681)
at com.my.dao.hibernate.TypeOneTypeTwoDaoHibernate.saveTypeOneTypeTwo(TypeOneTypeTwoDaoHibernate.java:68)
Name and version of the database you are using:
MySQL 4.1.20
The generated SQL (show_sql=true):
insert into T_TypeOne (TypeOne_ID, UUID, "NAME") values (null, ?, ?)
insert into T_TypeOne_TypeTwo (VERSION, DELETED, USER_ID, BEVENT_ID) values (?, ?, ?, ?)
|