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.  [ 4 posts ] 
Author Message
 Post subject: Help with many-to-many mapping table
PostPosted: Tue Jul 26, 2005 11:18 am 
Newbie

Joined: Thu Jul 07, 2005 1:47 pm
Posts: 6
Hibernate version:

3.0.2

I had originally set up a many-to-many relationship using the hibernate many-to-many mapping. This worked fine until I realized I needed to add an aditional field in that mapping table. Therefore I created the link table myself. Here is the hbm file for the link table.
Code:
<hibernate-mapping>

    <class name=Comp_Users" table="COMP_USERS">
        <id name="id" column="ID">
            <generator class="increment"/>
        </id>
        <property name="owner"/>
       
        <many-to-one name="company" column="companyid"
           class="Company"/>
          
          <many-to-one name="users" column="usersid"
             class="Users"/>
   </class>
   
</hibernate-mapping>


Here is my mapping in the users and company hbm files:
Company.hbm.xml:
Code:
     <!-- Old many-to-many
        <set name="users" table="comp_users">
           <key column="companyid"/>
           <many-to-many column="usersid" class="com.atwcorp.trustedapp.domain.useraccount.Users" />
        </set> 
     -->
        <set name="users" inverse="true">
           <key column="companyid"/>
           <one-to-many class="com.atwcorp.trustedapp.domain.useraccount.Comp_Users"/>
        </set>


Users.hbm.xml:
Code:
   <!--
        <set name="companys" table="comp_users">
           <key column="usersid"/>
           <many-to-many column="companyid" class="com.atwcorp.trustedapp.domain.company.Company"/>
        </set>
    -->
       <set name="companys" inverse="true">
          <key column="usersid"/>
          <one-to-many class="com.atwcorp.trustedapp.domain.useraccount.Comp_Users"/>
       </set>


Is there any code changes that need to take place since I added this link table on my own. I have created the appropriate java files for these three tables.

When I try to add a user to a company with this code now I get a cast exception:
Code:
public static Company createAndStoreCompany(String name, String url,
         Users user) {
      CompanyDAO dao = new CompanyDAO();

      Company company = new Company();
      HibernateUtil.getSession();
      HibernateUtil.beginTransaction();
      try {
         HashSet userSet = new HashSet();
         userSet.add(user);
         company.setName(name);
         company.setUrl(url);
         company.setUsers(userSet);
         dao.makePersistent(company);
         HibernateUtil.commitTransaction();
         HibernateUtil.closeSession();
      } catch (Exception e) {
         throw new InfrastructureException(e);
      }

      return (company);

   }


Exception:
Code:
15:12:48,671 ERROR [JTATransaction] JTA commit failed
org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=chiappone.trustwave.com/29, BranchQual=, localId=29] status=STATUS_NO_TRANSACTION; - nested throwable: (java.lang.ClassCastException: Users)
        at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:344)
        at org.jboss.tm.TxManager.commit(TxManager.java:200)
        at org.jboss.tm.usertx.client.ServerVMClientUserTransaction.commit(ServerVMClientUserTransaction.java:126)
        at org.hibernate.transaction.JTATransaction.commit(JTATransaction.java:130)
        at com.atwcorp.trustedapp.domain.util.HibernateUtil.commitTransaction(HibernateUtil.java:105)
        at com.atwcorp.trustedapp.domain.util.InitDatabase.createAndStoreCompany(InitDatabase.java:240)



Does anyone have an idea of what I'm doing wrong? Thanks...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 26, 2005 6:50 pm 
Regular
Regular

Joined: Fri Jan 28, 2005 3:11 am
Posts: 81
well, when you assign company.setUsers(userSet);
does each member of that userSet really belong to class Comp_Users?

If not, then that would be your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 26, 2005 6:51 pm 
Regular
Regular

Joined: Fri Jan 28, 2005 3:11 am
Posts: 81
in fact, you clearly see that it doesn't, being that you are passing in Users user, rather than Comp_Users user.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 26, 2005 8:19 pm 
Newbie

Joined: Tue May 24, 2005 7:08 pm
Posts: 17
Location: Melbourne
You will need a class (compUser.java) to represent your comp_users mapping. You then have a set of compUsers at each many end of the relationship.

I also find that having helper methods such as addCompUser(CompUser compUser) in your User (and Comp) class helps. This can create the relationship. Something like...

public void addCompUser(CompUser compUser) {
compUser.setUser(this);
getCompUsers().add(compUser);

}

You can also set up helper methods to traverse across the object graph and get Comps from User...

public Set getComps() {

Set comps = new HashSet();
for(Iterator it = this.getCompUsers();it.hasNext();) {
CompUser compUser = (CompUser) it.next();
Comp comp = compUser.getComp();
comps.add(comp)
}

return comps;
}

There are performance considerations in the above regarding the number of queries made to the database that may or may not be worth considering depending on your application.

One more thing is I put a <natural-id> tag around the foreign keys in my join tables...

in your case this would be the two sets - comps and users. This forces uniqueness on these two table columns.

Hope this helps... I don't have code in front of me at the moment so if there's syntax typos above I apologise but it should give you the general idea.

cheers,
rob


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.