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: update many-to-many relationship
PostPosted: Sat Feb 05, 2005 3:43 pm 
Newbie

Joined: Sat Feb 05, 2005 3:10 pm
Posts: 1
It is many-to-many associations like country, language situation. Say presently in a country five languages are spoken. So Country POJO contains the list of Language POJO.

Now suppose another language is being spoken, so I add
another element in the list for the country POJO to have six elements in the Language POJO. This language was existing in the language table. It is just the relationship has been added.

I am facing a problem here , when I add sixth element, hibernate fires delete for deleting the existing relationships and six inserts, rather
than firing just one insert.

How to configure correctly that hibernate fires only one insert ?

I tried with class cache at country level and collection cache for languages.

Hibernate version: 2.1.7

Mapping documents:

<bag name="languages" table="CountryLanguage" lazy="true" cascade="all">
<key column="CountryId"/>
<many-to-many class="com.common.business.LanguageBO" column="Languaged"/>
</bag>

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

try {
CountryBO country = null;

Session session = SessionFactoryUtils.getSession(getSessionFactory(), true);
country = (CountryBO)session.get(CountryBO.class, relation.getSourceCode());

Language result = (CountryBO)session.getNamedQuery("GetLanguageByCode").
setString("LanguageCode", langCode).
setCacheable(true).
uniqueResult();
List languages = country.getLanguages();
languages.add(result);
session.update(country);

} catch (HibernateException e) {
throw SessionFactoryUtils.convertHibernateAccessException(e);
}


Name and version of the database you are using:

Sybase

The generated SQL (show_sql=true):

Hibernate: delete from CountryLanguage where CountryId=?
Hibernate: insert into CountryLanguage (CountryId, LanguageId) values (?, ?)
Hibernate: insert into CountryLanguage (CountryId, LanguageId) values (?, ?)
Hibernate: insert into CountryLanguage (CountryId, LanguageId) values (?, ?)
Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject: collection be re-created
PostPosted: Mon Feb 07, 2005 3:30 am 
Newbie

Joined: Mon Feb 07, 2005 3:12 am
Posts: 2
Hi,
I also got the similar problem as metioned.
I found the solution at the faq sections as mentioned below.
http://www.hibernate.org/116.html#A14

************* text from the faq sections**************
Unlike other Hibernate value types, Hibernate tracks actual collection instances using Java identity, ==. Your getter method should return the same collection instance as was assigned by Hibernate to the setter method (unless you don't mind the collection being removed and recreated every time the session is flushed).
**************************************************

However, I really don't understand what they mean by "your getter method should return the same collection instance as was assigned by hibernate to the setter method"

I think my getter method already return the same collection.Below are my code

Code for getter and setter method

public class User {

private Set gameCharacter = new HashSet();

public void setGameCharacter(Set vec)
{
gameCharacter=vec;
}

public Set getGameCharacter()
{
return gameCharacter;
}

}

Can anyone guide me if there is anything wrong.


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.