Hibernate version: cvs snapshot 28/11/2005
So I had some problems with my many-to-many when I made a test case and realized that the many-to-many is not coupled in Hibernate.
Is this a feature, bug, or do I do something wrong?
Code:
group.getItems().add(item);
Above code will work. Data will be instereted and item/group will be associated.
The same things happends when you run:
Code:
item.getGroups().add(group);
However, if you have bound the associations from both ways, Hibernate will attempt to insert the same association twice:
Code:
public static final void main(String[] args) throws Exception {
new SchemaExport(HibernateUtil.getConfiguration()).create(true, true);
HibernateUtil.beginTransaction();
Group group = new Group();
HibernateUtil.getSession().save(group);
for (int i=10;i>0; i--) {
Item item = new Item();
item.getGroups().add(group);
group.getItems().add(item);
HibernateUtil.getSession().save(item);
}
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
}
Code:
/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/bin/java -Dfile.encoding=MacRoman -classpath /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/deploy.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/dt.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/jce.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/plugin.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/charsets.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/classes.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/dt.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/jce.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/jconsole.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/jsse.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/laf.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/ui.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/ext/apple_provider.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/ext/localedata.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/ext/sunjce_provider.jar:/Users/kalle/projects/snigel/myechelon/silvertejp testcase/classes:/Users/kalle/projects/other/hibernate-3.1/lib/ant-1.6.5.jar:/Users/kalle/projects/other/hibernate-3.1/lib/ant-antlr-1.6.5.jar:/Users/kalle/projects/other/hibernate-3.1/lib/ant-junit-1.6.5.jar:/Users/kalle/projects/other/hibernate-3.1/lib/ant-launcher-1.6.5.jar:/Users/kalle/projects/other/hibernate-3.1/lib/ant-swing-1.6.5.jar:/Users/kalle/projects/other/hibernate-3.1/lib/antlr-2.7.5H3.jar:/Users/kalle/projects/other/hibernate-3.1/lib/asm-attrs.jar:/Users/kalle/projects/other/hibernate-3.1/lib/asm.jar:/Users/kalle/projects/other/hibernate-3.1/lib/c3p0-0.9.0.jar:/Users/kalle/projects/other/hibernate-3.1/lib/cglib-2.1.2.jar:/Users/kalle/projects/other/hibernate-3.1/lib/cleanimports.jar:/Users/kalle/projects/other/hibernate-3.1/lib/commons-collections-2.1.1.jar:/Users/kalle/projects/other/hibernate-3.1/lib/commons-logging-1.0.4.jar:/Users/kalle/projects/other/hibernate-3.1/lib/concurrent-1.3.2.jar:/Users/kalle/projects/other/hibernate-3.1/lib/connector.jar:/Users/kalle/projects/other/hibernate-3.1/lib/dom4j-1.6.1.jar:/Users/kalle/projects/other/hibernate-3.1/lib/ehcache-1.1.jar:/Users/kalle/projects/other/hibernate-3.1/lib/jaas.jar:/Users/kalle/projects/other/hibernate-3.1/lib/jacc-1_0-fr.jar:/Users/kalle/projects/other/hibernate-3.1/lib/jboss-cache.jar:/Users/kalle/projects/other/hibernate-3.1/lib/jboss-common.jar:/Users/kalle/projects/other/hibernate-3.1/lib/jboss-jmx.jar:/Users/kalle/projects/other/hibernate-3.1/lib/jboss-system.jar:/Users/kalle/projects/other/hibernate-3.1/lib/jdbc2_0-stdext.jar:/Users/kalle/projects/other/hibernate-3.1/lib/jgroups-2.2.7.jar:/Users/kalle/projects/other/hibernate-3.1/lib/jta.jar:/Users/kalle/projects/other/hibernate-3.1/lib/junit-3.8.1.jar:/Users/kalle/projects/other/hibernate-3.1/lib/log4j-1.2.11.jar:/Users/kalle/projects/other/hibernate-3.1/lib/oscache-2.1.jar:/Users/kalle/projects/other/hibernate-3.1/lib/proxool-0.8.3.jar:/Users/kalle/projects/other/hibernate-3.1/lib/swarmcache-1.0rc2.jar:/Users/kalle/projects/other/hibernate-3.1/lib/syndiag2.jar:/Users/kalle/projects/other/hibernate-3.1/lib/versioncheck.jar:/Users/kalle/projects/other/hibernate-3.1/lib/xerces-2.6.2.jar:/Users/kalle/projects/other/hibernate-3.1/lib/xml-apis.jar:/Users/kalle/projects/other/hibernate-3.1/hibernate3.jar:/Users/kalle/maven/repository/log4j/jars/log4j-1.2.8.jar:/Users/kalle/maven/repository/commons-logging/jars/commons-logging-1.1-dev.jar:/Users/kalle/maven/repository/hsqldb/jars/hsqldb-1.7.3.3.jar se.snigel.testcase.Main
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
alter table Group_group_item_Item drop constraint FK9E24FD3FFC6B301E
alter table Group_group_item_Item drop constraint FK9E24FD3F68D7E4AA
drop table Group_group_item_Item if exists
drop table Item if exists
drop table _Group if exists
create table Group_group_item_Item (group_FK bigint not null, item_FK bigint not null, primary key (item_FK, group_FK))
create table Item (Item_PK bigint generated by default as identity (start with 1), primary key (Item_PK))
create table _Group (Group_PK bigint generated by default as identity (start with 1), primary key (Group_PK))
alter table Group_group_item_Item add constraint FK9E24FD3FFC6B301E foreign key (item_FK) references Item
alter table Group_group_item_Item add constraint FK9E24FD3F68D7E4AA foreign key (group_FK) references _Group
Hibernate: insert into _Group (Group_PK) values (null)
Hibernate: call identity()
Hibernate: insert into Item (Item_PK) values (null)
Hibernate: call identity()
Hibernate: insert into Item (Item_PK) values (null)
Hibernate: call identity()
Hibernate: insert into Item (Item_PK) values (null)
Hibernate: call identity()
Hibernate: insert into Item (Item_PK) values (null)
Hibernate: call identity()
Hibernate: insert into Item (Item_PK) values (null)
Hibernate: call identity()
Hibernate: insert into Item (Item_PK) values (null)
Hibernate: call identity()
Hibernate: insert into Item (Item_PK) values (null)
Hibernate: call identity()
Hibernate: insert into Item (Item_PK) values (null)
Hibernate: call identity()
Hibernate: insert into Item (Item_PK) values (null)
Hibernate: call identity()
Hibernate: insert into Item (Item_PK) values (null)
Hibernate: call identity()
Hibernate: insert into Group_group_item_Item (group_FK, item_FK) values (?, ?)
Hibernate: insert into Group_group_item_Item (group_FK, item_FK) values (?, ?)
Hibernate: insert into Group_group_item_Item (group_FK, item_FK) values (?, ?)
Hibernate: insert into Group_group_item_Item (group_FK, item_FK) values (?, ?)
Hibernate: insert into Group_group_item_Item (group_FK, item_FK) values (?, ?)
Hibernate: insert into Group_group_item_Item (group_FK, item_FK) values (?, ?)
Hibernate: insert into Group_group_item_Item (group_FK, item_FK) values (?, ?)
Hibernate: insert into Group_group_item_Item (group_FK, item_FK) values (?, ?)
Hibernate: insert into Group_group_item_Item (group_FK, item_FK) values (?, ?)
Hibernate: insert into Group_group_item_Item (group_FK, item_FK) values (?, ?)
Hibernate: insert into Group_group_item_Item (item_FK, group_FK) values (?, ?)
Exception in thread "main" java.lang.RuntimeException: org.hibernate.exception.ConstraintViolationException: could not insert collection: [se.snigel.testcase.Item.groups#1]
at se.snigel.testcase.HibernateUtil.commitTransaction(HibernateUtil.java:241)
at se.snigel.testcase.Main.main(Main.java:22)
Caused by: org.hibernate.exception.ConstraintViolationException: could not insert collection: [se.snigel.testcase.Item.groups#1]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1058)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:227)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:144)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:908)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:344)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at se.snigel.testcase.HibernateUtil.commitTransaction(HibernateUtil.java:233)
... 1 more
Caused by: java.sql.SQLException: Unique constraint violation: in statement [insert into Group_group_item_Item (item_FK, group_FK) values (?, ?)]
at org.hsqldb.jdbc.jdbcUtil.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1039)
... 11 more
Process finished with exit code 1
Mapping documents:item.hbm.xml
Code:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class
abstract="false"
dynamic-insert="true"
dynamic-update="true"
lazy="true"
name="se.snigel.testcase.Item"
table="Item">
<id
name="primaryKey"
column="Item_PK"
type="long"
unsaved-value="null">
<generator class="native"/>
</id>
<set
table="Group_group_item_Item"
name="groups">
<key column="item_FK"/>
<many-to-many
class="se.snigel.testcase.Group"
column="group_FK"/>
</set>
</class>
</hibernate-mapping>
group.hbm.xml
Code:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class
abstract="false"
dynamic-insert="true"
dynamic-update="true"
lazy="true"
name="se.snigel.testcase.Group"
table="_Group">
<id
name="primaryKey"
column="Group_PK"
type="long"
unsaved-value="null">
<generator class="native"/>
</id>
<set
table="Group_group_item_Item"
name="items">
<key column="group_FK"/>
<many-to-many
class="se.snigel.testcase.Item"
column="item_FK"/>
</set>
</class>
</hibernate-mapping>