-->
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 Java Problem
PostPosted: Sat Mar 27, 2010 12:01 pm 
Newbie

Joined: Sat Mar 27, 2010 11:51 am
Posts: 6
Hi People, I'm a newbie with hibernate and I found a problem with a relation many-to-many

I'm using netbeans 6.5 and I have generated the class and the mapping files for hibernate.

In the db I have made 3 table, "groups", "functionsToGroup" and "functions"; into "functionsToGroup" I have the reference to group id and functions id there relation is 0....N on both side.

Netbeans generate 2 java class "Groups.java" and "Functions.java" and put inside "Groups.java" a HashSet of functions and inside Functions.java " an hashset of Group.

In my java code , I try to

Code:
   session = startTransaction();
   Transaction transaction = session.getTransaction();
   Gruppo gruppo = (Gruppo) session.load(Gruppo.class, idGruppo);
                for (KeyValue kv : functions) {
                    Funzione fx =  (Funzione) session.createQuery("from Funzione where   CODICE_FUNZIONE=:code").setString("code", kv.getValue()).uniqueResult();
                    gruppo.getFunziones().add(fx);
                }
                             
                session.save(gruppo);
                transaction.commit();


I can see all the select made by hibernate engine, but I cannot find the insert into relation table (this's my target)

Code:
"Gruop.hbm..."
  <set name="funziones" inverse="true" table="funzione2gruppo" cascade="all">
            <key>
                <column name="ID_GRUPPO" not-null="true" />
            </key>
            <many-to-many entity-name="it.hibernate.beans.Funzione">
                <column name="ID_FUNZIONE" not-null="true" />
            </many-to-many>
        </set>

"Function.hbm..."
  <set name="gruppos" inverse="false" table="funzione2gruppo">
            <key>
                <column name="ID_FUNZIONE" not-null="true" />
            </key>
            <many-to-many entity-name="it.hibernate.beans.Gruppo">
                <column name="ID_GRUPPO" not-null="true" />
            </many-to-many>
        </set>



Where am I wrong?
Thanks a lot for your help


Top
 Profile  
 
 Post subject: Re: Many-to-many Java Problem
PostPosted: Sat Mar 27, 2010 1:39 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The inverse="true" in the <set> in Gruop.hbm means that this is read-only and Hibernate will not consider it for updates. You'll need to add the group to the set in functions instead. Eg.

Code:
fx.getGruppos().add(gruppo)


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.