I have a object which is supposed to return a list of Groups that the user is associated to. It is a many to many relationship.
I have a USER table, a GROUP table and a USER_GROUPS table.
My problem is that the method User.getUserGroups() is returning all the entries in the USER_GROUP table, not just the Groups where the USER_ID's match. I am therefore getting a lot of null objects.
The method that is having the problem is userGroups
Hibernate 3
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="uk.gov.ch.chips.common.user.User"
table="USER_ACCESS"
dynamic-update="false"
dynamic-insert="false"
>
<id name="id"
column="USER_ACCESS_ID"
type="java.lang.Long"
access="field">
<generator class="assigned" />
</id>
<property name="loginId"
column="LOGIN_ID"
type="string"
access="field"
length="30"
not-null="true"
/>
<property name="firstName"
column="USER_FORENAME"
type="string"
access="field"
length="50"
/>
<property name="lastName"
column="USER_SURNAME"
type="string"
access="field"
length="160"
/>
<property name="emailAddress"
column="USER_EMAIL"
type="string"
access="field"
length="256"
/>
<property name="telExt"
column="USER_TELEPHONE_NO"
type="string"
access="field"
/>
<property
name="locked"
type="yes_no"
update="true"
insert="true"
access="property"
column="Locked_IND"
length="1"
not-null="true"
/>
<property name="loginFailureCount"
column="LOGIN_FAILURE_COUNT"
type="int"
update="true"
insert="true"
access="property"
length="4"
not-null="true"
/>
<property
name="customStyleSheet"
column="CUSTOM_STYLESHEET"
type="string"
access="field"
length="64"
/>
<property
name="effectiveDate"
type="java.util.Date"
access="field"
column="USER_EFF_DATE"
length="10"
not-null="true"
/>
<property
name="endDate"
type="java.util.Date"
access="field"
column="USER_END_DATE"
length="10"
not-null="true"
/>
<property
name="tdfStart"
column="TDF_START"
type="java.lang.Integer"
access="field"
length="3"
/>
<property
name="tdfEnd"
column="TDF_END"
type="java.lang.Integer"
access="field"
length="3"
/>
<many-to-one name="homeOrgUnit"
class="uk.gov.ch.chips.common.user.OrgUnit"
access="field">
<column
name="ORGANISATIONAL_UNIT_ID"
/>
</many-to-one>
<list name="userPrivileges"
table="USER_PRIVILEGE"
lazy="true"
inverse="true"
access="field">
<key column="USER_ACCESS_ID" />
<index column="USER_PRIVILEGE_ID" />
<many-to-many class="uk.gov.ch.chips.common.user.Privilege"
column="PRIVILEGE_TYPE_ID" />
</list>
<list name="userGroups"
table="USERACC_GROUPACC_LINK"
lazy="true"
inverse="true"
access="field"
cascade="save-update">
<key column="USER_ACCESS_ID" />
<index column="USER_GROUPACC_LINK_ID" />
<many-to-many class="uk.gov.ch.chips.common.user.Group"
column="GROUP_ACCESS_ID" />
</list>
</class>
<!--
LoggedInUser is a subclass of User, and is included in the User
Hibernate mapping file to allow a LoggedInUser to be saved in
the database. Polymorphism must be set to "explicit". All property
files that are needed to save a User into the USER_ACCESS table must
also be included in the LoggedInUser mappings.
-->
<class
name="uk.gov.ch.chips.common.user.LoggedInUser"
table="USER_ACCESS"
dynamic-update="false"
dynamic-insert="false"
polymorphism="explicit"
>
<id name="id"
column="USER_ACCESS_ID"
type="java.lang.Long"
access="field">
<generator class="assigned" />
</id>
<property name="loginId"
column="LOGIN_ID"
type="string"
access="field"
length="30"
not-null="true"
/>
<property name="firstName"
column="USER_FORENAME"
type="string"
access="field"
length="50"
/>
<property name="lastName"
column="USER_SURNAME"
type="string"
access="field"
length="160"
/>
<property name="emailAddress"
column="USER_EMAIL"
type="string"
access="field"
length="256"
/>
<property name="telExt"
column="USER_TELEPHONE_NO"
type="string"
access="field"
/>
<property
name="locked"
type="yes_no"
access="property"
column="Locked_IND"
length="1"
not-null="true"
/>
<property name="loginFailureCount"
column="LOGIN_FAILURE_COUNT"
type="int"
access="property"
length="4"
not-null="true"
/>
<property
name="customStyleSheet"
column="CUSTOM_STYLESHEET"
type="string"
access="field"
length="64"
/>
<property
name="effectiveDate"
type="java.util.Date"
access="field"
column="USER_EFF_DATE"
length="10"
not-null="true"
/>
<property
name="endDate"
type="java.util.Date"
access="field"
column="USER_END_DATE"
length="10"
not-null="true"
/>
<property
name="tdfStart"
column="TDF_START"
type="java.lang.Integer"
access="field"
length="3"
/>
<property
name="tdfEnd"
column="TDF_END"
type="java.lang.Integer"
access="field"
length="3"
/>
<many-to-one name="homeOrgUnit"
class="uk.gov.ch.chips.common.user.OrgUnit"
access="field">
<column
name="ORGANISATIONAL_UNIT_ID"
/>
</many-to-one>
<list name="userPrivileges"
table="USER_PRIVILEGE"
lazy="true"
access="field">
<key column="USER_ACCESS_ID" />
<index column="USER_PRIVILEGE_ID" />
<many-to-many class="uk.gov.ch.chips.common.user.Privilege"
column="PRIVILEGE_TYPE_ID" />
</list>
<list name="userGroups"
table="USERACC_GROUPACC_LINK"
lazy="true"
access="field"
cascade="save-update">
<key column="USER_ACCESS_ID" />
<index column="USER_GROUPACC_LINK_ID" />
<many-to-many class="uk.gov.ch.chips.common.user.Group"
column="GROUP_ACCESS_ID" />
</list>
</class>
</hibernate-mapping>
Associated Class:
package uk.gov.ch.chips.common.user;
import java.util.Date;
import java.util.List;
import java.io.Serializable;
import uk.gov.ch.chips.common.validation.Validatible;
import uk.gov.ch.chips.common.validation.ValidationException;
import uk.gov.ch.chips.common.validation.Validator;
/**
* Represents a user, as required in the CHIPS user model.
*
* @author mwestacott
*/
public class User implements Serializable, Validatible {
protected Long id;
protected String loginId;
protected OrgUnit homeOrgUnit;
protected String firstName;
protected String lastName;
protected String location;
protected String emailAddress;
protected String telExt;
protected String password;
protected boolean isTeamLeader;
protected boolean locked;
protected int loginFailureCount;
protected Date effectiveDate;
protected Date endDate;
protected List userPrivileges;
protected List userGroups;
protected String customStyleSheet;
protected Integer tdfStart;
protected Integer tdfEnd;
/**
* Default no arguments constructor.
*/
public User() {}
public User(Long id,
String loginId,
OrgUnit homeOrgUnit,
String firstName,
String lastName,
String location,
String emailAddress,
String telExt,
String password,
boolean isTeamLeader,
boolean locked,
int loginFailureCount,
Date effectiveDate,
Date endDate,
String customStyleSheet,
Integer tdfStart,
Integer tdfEnd) {
this.id = id;
this.loginId = loginId;
this.homeOrgUnit = homeOrgUnit;
this.firstName = firstName;
this.lastName = lastName;
this.location = location;
this.emailAddress = emailAddress;
this.telExt = telExt;
this.password = password;
this.locked = locked;
this.loginFailureCount = loginFailureCount;
this.effectiveDate = effectiveDate;
this.endDate = endDate;
this.customStyleSheet = customStyleSheet;
this.tdfStart = tdfStart;
this.tdfEnd = tdfEnd;
// TODO Uncomment when unit-test have been fixed
//doValidation();
}
/**
* @deprecated Use the other constructor and supply a team leader status. Intention is
* to delete this constructor once existing code has been refactored.
*/
public User(Long id,
String loginId,
OrgUnit homeOrgUnit,
String firstName,
String lastName,
String location,
String emailAddress,
String telExt,
String password) {
this(id, loginId, homeOrgUnit, firstName, lastName, location, emailAddress, telExt, password, false);
}
/**
* @deprecated Use the other constructor and supply a isLocked and loginFailures. Intention is
* to delete this constructor once existing code has been refactored.
*/
public User(Long id,
String loginId,
OrgUnit homeOrgUnit,
String firstName,
String lastName,
String location,
String emailAddress,
String telExt,
String password,
boolean isTeamLeader) {
this(id, loginId, homeOrgUnit, firstName, lastName, location, emailAddress, telExt, password, false, false, 0, null, null);
}
/**
* @deprecated Use the other constructor and supply a isLocked and loginFailures. Intention is
* to delete this constructor once existing code has been refactored.
*/
public User(Long id,
String loginId,
OrgUnit homeOrgUnit,
String firstName,
String lastName,
String location,
String emailAddress,
String telExt,
String password,
boolean isTeamLeader,
boolean locked,
int loginFailureCount,
Date effectiveDate,
Date endDate) {
this.id = id;
this.loginId = loginId;
this.homeOrgUnit = homeOrgUnit;
this.firstName = firstName;
this.lastName = lastName;
this.location = location;
this.emailAddress = emailAddress;
this.telExt = telExt;
this.password = password;
this.locked = locked;
this.loginFailureCount = loginFailureCount;
this.effectiveDate = effectiveDate;
this.endDate = endDate;
// TODO Uncomment when unit-test have been fixed
// doValidation();
}
/**
* @deprecated Use the other constructor and supply a isLocked and loginFailures. Intention is
* to delete this constructor once existing code has been refactored.
*/
public User(Long id,
String loginId,
OrgUnit homeOrgUnit,
String firstName,
String lastName,
String location,
String emailAddress,
String telExt,
String password,
boolean isTeamLeader,
boolean locked,
int loginFailureCount,
Date effectiveDate,
Date endDate,
String customStyleSheet) {
this.id = id;
this.loginId = loginId;
this.homeOrgUnit = homeOrgUnit;
this.firstName = firstName;
this.lastName = lastName;
this.location = location;
this.emailAddress = emailAddress;
this.telExt = telExt;
this.password = password;
this.locked = locked;
this.loginFailureCount = loginFailureCount;
this.effectiveDate = effectiveDate;
this.endDate = endDate;
this.customStyleSheet = customStyleSheet;
// TODO Uncomment when unit-test have been fixed
//doValidation();
}
// This method (and its invocation in the above constructor) is a nasty work-around
// to the problem of validating objects in an inheritance hierarchy. It can be overridden
// in a sub-class to delay validation until an object is fully constructed.
protected void doValidation() {
Validator.validate(this);
}
/**
* @see uk.gov.ch.chips.common.validation.Validatible#validate(uk.gov.ch.chips.common.validation.Validator)
*/
public void validate(Validator validator) throws ValidationException {
// TODO More validation needed...
validator.checkNotNull(this.loginId, "Login id is null");
validator.checkNotNull(this.homeOrgUnit, "Home org unit is null");
if (this.homeOrgUnit != null) {
this.homeOrgUnit.validate(validator);
}
}
public Long getId() {
return this.id;
}
public String getLoginId() {
return this.loginId;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
public String getEmailAddress() {
return this.emailAddress;
}
public String getTelExt() {
return this.telExt;
}
public String getPassword() {
return this.password;
}
public OrgUnit getHomeOrgUnit() {
return this.homeOrgUnit;
}
public boolean isTeamLeader() {
return this.isTeamLeader;
}
public boolean getLocked() {
return this.locked;
}
public void setLocked(boolean locked) {
this.locked = locked;
}
public int getLoginFailureCount() {
return this.loginFailureCount;
}
public void setLoginFailureCount(int loginFailureCount) {
this.loginFailureCount = loginFailureCount;
}
public Date getEffectiveDate() {
return this.effectiveDate;
}
public Date getEndDate() {
return this.endDate;
}
public String getCustomStyleSheet(){
return this.customStyleSheet;
}
public Integer getTdfStart(){
return this.tdfStart;
}
public Integer getTdfEnd(){
return this.tdfEnd;
}
public List getUserPrivileges() {
return this.userPrivileges;
}
public List getUserGroups() {
return this.userGroups;
}
}
Thanks
|