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