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...