-->
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: composite-id
PostPosted: Tue Jan 27, 2004 12:20 am 
Newbie

Joined: Sun Nov 23, 2003 3:24 pm
Posts: 1
Hi,

I'm having trouble using a <composite-id>. I've got four classes: User, Account, Role and AccountUser. I can save, update, restore and delete Users, Accounts and Roles using Hibernate, but I run into trouble with my AccountUser class. The idea is that a User may have several accounts, but only one role in any given account, so AccountUser has a User, an Account and a Role and this is all described as follows:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping.dtd">
<hibernate-mapping>
   <class name="AccountUser" table="account_user">
      <composite-id>
         <key-property name="account"/>
         <key-property name="user"/>
      </composite-id>
      <property
         name="account"                               
         column="account_id"                               
         type="org.chelimsky.tools.user.Account"                                 
         not-null="true"                   
      />
      <property
         name="user"                 
         column="user_id"               
         type="org.chelimsky.tools.user.User"
         not-null="true"                   
      />
      <property
         name="role"                               
         column="role_id"                               
         type="org.chelimsky.tools.user.Role"                                 
         not-null="true"                   
      />
   </class>
</hibernate-mapping>


The create statement for the table looks like this (db is mysql):

Code:
CREATE TABLE account_user (
   account_id int(11) NOT NULL,
   user_id int(11) NOT NULL,
   role_id int(11) NOT NULL,
   FOREIGN KEY (account_id) REFERENCES account,
   FOREIGN KEY (user_id) REFERENCES user,
   FOREIGN KEY (role_id) REFERENCES role,
   PRIMARY KEY (account_id, user_id)
);


Here's the AccountUser class - note that it implements Serializable and overrides hashCode and equals:
Code:
package org.chelimsky.tools.user;

import java.io.Serializable;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
* Associates a User with an Account and a Role
* for that Account. Single public constructor
* requires all three properties (user, account, role)
* but only additional setter is role - the idea being
* that you can create an instance and change its role -
* but that the fundamental identity of the UserAccount
* is defined by the Account and the User.
*/
public class AccountUser implements Serializable {

   private User user;
   private Account account;
   private Role role;
   
   public AccountUser(User user, Account account, Role role) {
      this.user = user;
      this.account = account;
      this.role = role;
   }

   public AccountUser() {}


   
   /* (non-Javadoc)
    * @see java.lang.Object#equals(java.lang.Object)
    */
   public boolean equals(Object arg0) {
      AccountUser that = (AccountUser) arg0;
      return new EqualsBuilder()
         .append(this.user, that.getUser())
         .append(this.account, that.getAccount())
         .append(this.role, that.getRole())
         .isEquals();
   }

   /* (non-Javadoc)
    * @see java.lang.Object#toString()
    */
   public String toString() {
      return new ToStringBuilder(this)
         .append("user", this.user)
         .append("account",this.account)
         .append("roles", this.role)
         .toString();
   }
   
   /* (non-Javadoc)
    * @see java.lang.Object#hashCode()
    */
   public int hashCode() {
      return new HashCodeBuilder()
      .append(this.user)
      .append(this.account)
      .append(this.role)
      .hashCode();
   }

   /**
    * @return
    */
   public Account getAccount() {
      return account;
   }

   /**
    * @return
    */
   public Role getRole() {
      return role;
   }

   /**
    * @return
    */
   public User getUser() {
      return user;
   }

   /**
    * @param account
    */
   public void setAccount(Account account) {
      this.account = account;
   }

   /**
    * @param role
    */
   public void setRole(Role role) {
      this.role = role;
   }

   /**
    * @param user
    */
   public void setUser(User user) {
      this.user = user;
   }

}


When I add the above to the mix, I get a java.lang.ExceptionInInitializerError. I'm using hibernate-2.0.1 with mysql-4.0.16. Any help would be appreciated.

Thanks,
David


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 4:56 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
full stacktrace and log would be helpfull.

have a look at <key-many-to-one/>, it fits your needs

_________________
Emmanuel


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.