Sorry - this was meant to be in the Hibernate Users forum.
I have user and role tables with a junction table that has the primary keys of the usr_id and role_id, along with some extra fields. This is very similar to what is described in
http://forum.hibernate.org/viewtopic.php?t=959196&highlight=compositeelement and in section 6.3.2 of Hibernate in Action (p 229). I have used a composite element to map the extra fields, which are the username and date created.
User declares a Set of Roles:
Code:
private Set<Role> roles = new LinkedHashSet<Role>();
and has a public getter and setter for the Roles Set and also an addRole method which takes a Role object as its parameter.
The extra java class used in the composite-element is as follows:
Code:
public class UserRole implements Serializable {
private User user;
private Role role;
private String userIdCreate = Utils.getUsername();
private Date dateCreate = new Date();
public UserRole() {
}
public Date getDateCreate() {
return dateCreate;
}
public Role getRole() {
return role;
}
public User getUser() {
return user;
}
public String getUserIdCreate() {
return userIdCreate;
}
public void setUser(User user) {
this.user = user;
}
public void setRole(Role role) {
this.role = role;
}
}
The mapping for User has this:
Code:
<hibernate-mapping auto-import="true" default-lazy="false">
<class name="au.com.woolworths.hrportal.user.User" table="USR">
...
<set name="roles" table="USR_ROLE_XREF" cascade="all" access="field" lazy="true">
<key column="USR_ID" not-null="true"/>
<composite-element class="au.com.woolworths.hrportal.user.component.UserRole">
<many-to-one name="role" class="au.com.woolworths.hrportal.user.role.Role" column="ROLE_ID" access="field"/>
<property name="userIdCreate" column="CRE_USR_ID" />
<property name="dateCreate" column="CRE_DTETM" />
</composite-element>
</set>
...
When I try to cerate a new User containing Roles, I get the following exception:
Code:
org.springframework.orm.hibernate3.HibernateSystemException: could not get a field value by reflection getter of au.com.woolworths.hrportal.user.component.UserRole.role; nested exception is org.hibernate.PropertyAccessException: could not get a field value by reflection getter of au.com.woolworths.hrportal.user.component.UserRole.role
I've been struggling with this all day and I don't know why it doesn't work.
Any suggestions would be appreciated
Thanks
Alan