Hello,
I have a parent child relationship between two objects User and UserAttribute. There is a test below where I try to update an attribute that is associated with a user. However, instead of updating the record, hibernate is deleting the records. Any ideas on what I am doing wrong?
thanks for you help
Hibernate version:3.2.3ga
Mapping documents:
Code:
<hibernate-mapping default-lazy="true">
<class name="org.openiam.idm.srvc.user.dto.User" table="USERS" >
<comment></comment>
<id name="userId" type="string">
<column name="USER_ID" length="20" />
<generator class="assigned" />
</id>
<property name="firstName" type="string">
<column name="FIRST_NAME" length="40">
<comment></comment>
</column>
</property>
<property name="lastName" type="string">
<column name="LAST_NAME" length="40">
<comment></comment>
</column>
</property>
<property name="middleInit" type="java.lang.Character">
<column name="MIDDLE_INIT" length="1">
<comment></comment>
</column>
</property>
<property name="title" type="string">
<column name="TITLE" length="30">
<comment></comment>
</column>
</property>
<property name="dept" type="string">
<column name="DEPT" length="20">
<comment></comment>
</column>
</property>
<property name="status" type="string">
<column name="STATUS" length="20">
<comment></comment>
</column>
</property>
<property name="birthdate" type="timestamp">
<column name="BIRTHDATE" length="19">
<comment></comment>
</column>
</property>
<property name="sex" type="java.lang.Character">
<column name="SEX" length="1">
<comment></comment>
</column>
</property>
<property name="createDate" type="timestamp">
<column name="CREATE_DATE" length="19">
<comment></comment>
</column>
</property>
<property name="createdBy" type="string">
<column name="CREATED_BY" length="20">
<comment></comment>
</column>
</property>
<property name="lastUpdate" type="timestamp">
<column name="LAST_UPDATE" length="19">
<comment></comment>
</column>
</property>
<property name="lastUpdatedBy" type="string">
<column name="LAST_UPDATED_BY" length="20">
<comment></comment>
</column>
</property>
<property name="prefix" type="string">
<column name="PREFIX" length="4">
<comment></comment>
</column>
</property>
<property name="suffix" type="string">
<column name="SUFFIX" length="20">
<comment></comment>
</column>
</property>
<property name="userTypeInd" type="string">
<column name="USER_TYPE_IND" length="20">
<comment></comment>
</column>
</property>
<property name="employeeId" type="string">
<column name="EMPLOYEE_ID" length="20">
<comment></comment>
</column>
</property>
<property name="employeeType" type="string">
<column name="EMPLOYEE_TYPE" length="20">
<comment></comment>
</column>
</property>
<property name="locationId" type="string">
<column name="LOCATION_ID" length="20">
<comment></comment>
</column>
</property>
<property name="companyId" type="string">
<column name="COMPANY_ID" length="20">
<comment></comment>
</column>
</property>
<property name="companyOwnerId" type="string">
<column name="COMPANY_OWNER_ID" length="20">
<comment></comment>
</column>
</property>
<property name="expirationDate" type="timestamp">
<column name="EXPIRATION_DATE" length="19">
<comment></comment>
</column>
</property>
<property name="managerId" type="string">
<column name="MANAGER_ID" length="20">
<comment></comment>
</column>
</property>
<property name="jobCode" type="string">
<column name="JOB_CODE" length="20">
<comment></comment>
</column>
</property>
<set name="userAttributes" inverse="true" cascade="all-delete-orphan" lazy="false" >
<key>
<column name="USER_ID" length="20">
<comment></comment>
</column>
</key>
<one-to-many class="org.openiam.idm.srvc.user.dto.UserAttribute" />
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="org.openiam.idm.srvc.user.dto.UserAttribute" table="user_attributes">
<comment></comment>
<id name="id" type="string">
<column name="ID" length="20" />
<generator class="assigned" />
</id>
<many-to-one name="users" class="org.openiam.idm.srvc.user.dto.User" fetch="select">
<column name="USER_ID" length="20">
<comment></comment>
</column>
</many-to-one>
<property name="name" type="string">
<column name="NAME" length="20">
<comment></comment>
</column>
</property>
<property name="value" type="string">
<column name="VALUE">
<comment></comment>
</column>
</property>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():JUnit Test Code
Code:
public void testSaveAttribute() {
User usr = userMgr.getUser(user.getUserId(), true);
Set<UserAttribute> attrSet = usr.getUserAttributes();
Iterator<UserAttribute> it = attrSet.iterator();
if (it.hasNext()) {
UserAttribute at = it.next();
at.setValue("updated val");
attrSet.add( at);
usr.setUserAttributes(attrSet);
userMgr.updateUser(user);
}
}
Service Bean Code
Code:
public void updateUser(User user) {
if (user == null)
throw new NullPointerException("user object is null");
if (user.getUserId() == null)
throw new NullPointerException("user id is null");
userDao.merge(user);
}
DAO code:
Code:
public User merge(User detachedInstance) {
log.debug("merging User instance");
try {
User result = (User) sessionFactory.getCurrentSession().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
entity Code:
public class User extends org.openiam.base.BaseObject {
// Fields
private String userId;
private MetadataType metadataType;
private String firstName;
private String lastName;
private Character middleInit;
private String title;
private String dept;
private String status;
private Date birthdate;
private Character sex;
private Date createDate;
private String createdBy;
private Date lastUpdate;
private String lastUpdatedBy;
private String prefix;
private String suffix;
private String userTypeInd;
private String employeeId;
private String employeeType;
private String locationId;
private String companyId;
private String companyOwnerId;
private Date expirationDate;
private String managerId;
private String jobCode;
private Set userGrps = new HashSet(0);
private Set userNotes = new HashSet(0);
private Set credentialses = new HashSet(0);
private Set authStates = new HashSet(0);
private Set userIdentityAnses = new HashSet(0);
private Set resourceUsers = new HashSet(0);
private Set logins = new HashSet(0);
private Set roles = new HashSet(0);
//private Map userAttributes = new HashMap();
private Set userAttributes = new HashSet(0);
// Constructors
/** default constructor */
public User() {
}
/** minimal constructor */
public User(String userId) {
this.userId = userId;
}
/** full constructor */
public User(String userId, MetadataType metadataType, String firstName,
String lastName, Character middleInit, String title, String dept,
String status, Date birthdate, Character sex, Date createDate,
String createdBy, Date lastUpdate, String lastUpdatedBy,
String prefix, String suffix, String userTypeInd,
String employeeId, String employeeType, String locationId,
String companyId, String companyOwnerId, Date expirationDate,
String managerId, String jobCode, Set userGrps, Set userNotes,
Set credentialses, Set authStates, Set userIdentityAnses,
Set resourceUsers, Set logins, Set roles, Set userAttributes) {
this.userId = userId;
this.metadataType = metadataType;
this.firstName = firstName;
this.lastName = lastName;
this.middleInit = middleInit;
this.title = title;
this.dept = dept;
this.status = status;
this.birthdate = birthdate;
this.sex = sex;
this.createDate = createDate;
this.createdBy = createdBy;
this.lastUpdate = lastUpdate;
this.lastUpdatedBy = lastUpdatedBy;
this.prefix = prefix;
this.suffix = suffix;
this.userTypeInd = userTypeInd;
this.employeeId = employeeId;
this.employeeType = employeeType;
this.locationId = locationId;
this.companyId = companyId;
this.companyOwnerId = companyOwnerId;
this.expirationDate = expirationDate;
this.managerId = managerId;
this.jobCode = jobCode;
this.userGrps = userGrps;
this.userNotes = userNotes;
this.credentialses = credentialses;
this.authStates = authStates;
this.userIdentityAnses = userIdentityAnses;
this.resourceUsers = resourceUsers;
this.logins = logins;
this.roles = roles;
this.userAttributes = userAttributes;
}
// Property accessors
public String getUserId() {
return this.userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public MetadataType getMetadataType() {
return this.metadataType;
}
public void setMetadataType(MetadataType metadataType) {
this.metadataType = metadataType;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Character getMiddleInit() {
return this.middleInit;
}
public void setMiddleInit(Character middleInit) {
this.middleInit = middleInit;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDept() {
return this.dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getStatus() {
return this.status;
}
public void setStatus(String status) {
this.status = status;
}
public Date getBirthdate() {
return this.birthdate;
}
public void setBirthdate(Date birthdate) {
this.birthdate = birthdate;
}
public Character getSex() {
return this.sex;
}
public void setSex(Character sex) {
this.sex = sex;
}
public Date getCreateDate() {
return this.createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public String getCreatedBy() {
return this.createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public Date getLastUpdate() {
return this.lastUpdate;
}
public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}
public String getLastUpdatedBy() {
return this.lastUpdatedBy;
}
public void setLastUpdatedBy(String lastUpdatedBy) {
this.lastUpdatedBy = lastUpdatedBy;
}
public String getPrefix() {
return this.prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return this.suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public String getUserTypeInd() {
return this.userTypeInd;
}
public void setUserTypeInd(String userTypeInd) {
this.userTypeInd = userTypeInd;
}
public String getEmployeeId() {
return this.employeeId;
}
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeType() {
return this.employeeType;
}
public void setEmployeeType(String employeeType) {
this.employeeType = employeeType;
}
public String getLocationId() {
return this.locationId;
}
public void setLocationId(String locationId) {
this.locationId = locationId;
}
public String getCompanyId() {
return this.companyId;
}
public void setCompanyId(String companyId) {
this.companyId = companyId;
}
public String getCompanyOwnerId() {
return this.companyOwnerId;
}
public void setCompanyOwnerId(String companyOwnerId) {
this.companyOwnerId = companyOwnerId;
}
public Date getExpirationDate() {
return this.expirationDate;
}
public void setExpirationDate(Date expirationDate) {
this.expirationDate = expirationDate;
}
public String getManagerId() {
return this.managerId;
}
public void setManagerId(String managerId) {
this.managerId = managerId;
}
public String getJobCode() {
return this.jobCode;
}
public void setJobCode(String jobCode) {
this.jobCode = jobCode;
}
public Set getUserGrps() {
return this.userGrps;
}
public void setUserGrps(Set userGrps) {
this.userGrps = userGrps;
}
public Set getUserNotes() {
return this.userNotes;
}
public void setUserNotes(Set userNotes) {
this.userNotes = userNotes;
}
public Set getCredentialses() {
return this.credentialses;
}
public void setCredentialses(Set credentialses) {
this.credentialses = credentialses;
}
public Set getAuthStates() {
return this.authStates;
}
public void setAuthStates(Set authStates) {
this.authStates = authStates;
}
public Set getUserIdentityAnses() {
return this.userIdentityAnses;
}
public void setUserIdentityAnses(Set userIdentityAnses) {
this.userIdentityAnses = userIdentityAnses;
}
public Set getResourceUsers() {
return this.resourceUsers;
}
public void setResourceUsers(Set resourceUsers) {
this.resourceUsers = resourceUsers;
}
public Set getLogins() {
return this.logins;
}
public void setLogins(Set logins) {
this.logins = logins;
}
public Set getRoles() {
return this.roles;
}
public void setRoles(Set roles) {
this.roles = roles;
}
public Set getUserAttributes() {
return this.userAttributes;
}
public void setUserAttributes(Set userAttributeses) {
this.userAttributes = userAttributeses;
}
}
public class UserAttribute implements java.io.Serializable{
// Fields
private String id;
private User users;
private MetadataElement metadataElement;
private String name;
private String value;
// Constructors
/** default constructor */
public UserAttribute() {
}
/** minimal constructor */
public UserAttribute(String id) {
this.id = id;
}
/** full constructor */
public UserAttribute(String id, User users,
MetadataElement metadataElement, String name, String value) {
this.id = id;
this.users = users;
this.metadataElement = metadataElement;
this.name = name;
this.value = value;
}
// Property accessors
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public User getUsers() {
return this.users;
}
public void setUsers(User users) {
this.users = users;
}
public MetadataElement getMetadataElement() {
return this.metadataElement;
}
public void setMetadataElement(MetadataElement metadataElement) {
this.metadataElement = metadataElement;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
Name and version of the database you are using:MySQLThe generated SQL (show_sql=true):Code:
Hibernate: select user0_.USER_ID as USER1_102_1_, user0_.FIRST_NAME as FIRST2_102_1_, user0_.LAST_NAME as LAST3_102_1_, user0_.MIDDLE_INIT as MIDDLE4_102_1_, user0_.TITLE as TITLE102_1_, user0_.DEPT as DEPT102_1_, user0_.STATUS as STATUS102_1_, user0_.BIRTHDATE as BIRTHDATE102_1_, user0_.SEX as SEX102_1_, user0_.CREATE_DATE as CREATE10_102_1_, user0_.CREATED_BY as CREATED11_102_1_, user0_.LAST_UPDATE as LAST12_102_1_, user0_.LAST_UPDATED_BY as LAST13_102_1_, user0_.PREFIX as PREFIX102_1_, user0_.SUFFIX as SUFFIX102_1_, user0_.USER_TYPE_IND as USER16_102_1_, user0_.EMPLOYEE_ID as EMPLOYEE17_102_1_, user0_.EMPLOYEE_TYPE as EMPLOYEE18_102_1_, user0_.LOCATION_ID as LOCATION19_102_1_, user0_.COMPANY_ID as COMPANY20_102_1_, user0_.COMPANY_OWNER_ID as COMPANY21_102_1_, user0_.EXPIRATION_DATE as EXPIRATION22_102_1_, user0_.MANAGER_ID as MANAGER23_102_1_, user0_.JOB_CODE as JOB24_102_1_, userattrib1_.USER_ID as USER2_3_, userattrib1_.ID as ID3_, userattrib1_.ID as ID104_0_, userattrib1_.USER_ID as USER2_104_0_, userattrib1_.NAME as NAME104_0_, userattrib1_.VALUE as VALUE104_0_ from USERS user0_ left outer join user_attributes userattrib1_ on user0_.USER_ID=userattrib1_.USER_ID where user0_.USER_ID=?
Code:
Hibernate: delete from user_attributes where ID=?
Hibernate: delete from user_attributes where ID=?