-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: many-to-many insert problem
PostPosted: Tue Feb 06, 2007 6:35 am 
Newbie

Joined: Tue Feb 06, 2007 6:18 am
Posts: 2
Hibernate version: 3
Mapping documents:

<hibernate-mapping>
<class name="it.reply.wom.Capability" table="CAPABILITY" schema="WOM2" dynamic-insert="true" dynamic-update="true">
<id name="idCapability" type="string">
<column name="ID_CAPABILITY" length="32" />
<generator class="assigned" />
</id>
<property name="description" type="string">
<column name="DESCRIPTION" length="50" not-null="true" />
</property>
<set name="profiles" inverse="true" table="PROFILE_CAPABILITY" cascade="save-update">
<key>
<column name="ID_CAPABILITY" length="32" not-null="true" />
</key>
<many-to-many entity-name="it.reply.wom.Profile">
<column name="PROFILE_NAME" length="32" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>



<hibernate-mapping>
<class name="it.reply.wom.Profile" table="PROFILE" schema="WOM2" dynamic-insert="true" dynamic-update="true">
<id name="profileName" type="string">
<column name="PROFILE_NAME" length="32" />
<generator class="assigned" />
</id>
<property name="sincro" type="char">
<column name="SINCRO" length="1" not-null="true" />
</property>
<property name="description" type="string">
<column name="DESCRIPTION" length="50" not-null="true" />
</property>
<set name="womUsers" inverse="true" table="USER_PROFILE" cascade="save-update">
<key>
<column name="PROFILE_NAME" length="32" not-null="true" />
</key>
<many-to-many entity-name="it.reply.wom.WomUser">
<column name="USER_ID" length="32" not-null="true" />
</many-to-many>
</set>
<set name="capabilities" inverse="true" table="PROFILE_CAPABILITY" cascade="save-update">
<key>
<column name="PROFILE_NAME" length="32" not-null="true" />
</key>
<many-to-many entity-name="it.reply.wom.Capability">
<column name="ID_CAPABILITY" length="32" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>





Code between sessionFactory.openSession() and session.close():


Session session = HibernateUtil.currentSession();

session.beginTransaction();

Capability capab1 = new Capability();
capab1.setIdCapability("c4");
capab1.setDescription("d2");
session.save(capab1);

Profile profile1 = new Profile();
profile1.setProfileName("P4");
profile1.setDescription("d2");
profile1.setSincro('1');
session.save(profile1);

profile1.getCapabilities().add(capab1);
capab1.getProfiles().add(profile1);

session.update(profile1);
session.update(capab1);

session.getTransaction().commit();
HibernateUtil.closeSession();


Full stack trace of any exception that occurs: NO EXECTION THROWS
Name and version of the database you are using: Oracle 9



Hi
I'm new with Hibernate
I have a relation many-to-many between two entity described in hbm file described above.
HBM mapping files are generated with Hibernate tool.

The java code doesn't throw any exception but when I commit tansaction I have ony 1 record in the PROFILE table and 1 record in the CAPABILITY table but none in the PROFILE_CAPABILITY table.

what go wrong?

thanks a lot


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 12:51 pm 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
Hi,

Both of your sets for the many-to-many mapping have "inverse" set to true.

What the "inverse" does is that it tells hibernate to ignore it. Consider it a mirror of the other side. Since both sides have it set to true, nothing happens to the mapping (the PROFILE_CAPABILITY table is not updated). Likewise, if both sides are set to false, it will try to insert into the PROFILE_CAPABILITY table twice and you'd get an exception in that case.

So try setting inverse="false".

In fact you don't need the session.updates() in your code. You should be able to achieve what you need with the following:

Code:
Capability capab1 = new Capability();
capab1.setIdCapability("c4");
capab1.setDescription("d2");

Profile profile1 = new Profile();
profile1.setProfileName("P4");
profile1.setDescription("d2");
profile1.setSincro('1');

profile1.getCapabilities().add(capab1);
capab1.getProfiles().add(profile1);

session.save(profile1);


You don't even need to save capab1. The save of profile1 will cascade to the save of capab1 and should create the entry in PROFILE_CAPABILITY table since you have cascade="save-update"

Budyanto


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.