Dear forum users, --
please do not think TL;DR--
I wish to better understand how the session.get(...) method works, in case of an entity with composite id.
I have a class PcTaskAssignee.java , defined as follows :
Code:
package previtc.model.task;
import java.io.Serializable;
import org.apache.commons.lang.builder.HashCodeBuilder;
import previtc.model.AssigneeCompare;
import previtc.model.EntityObject;
public class PcTaskAssignee extends EntityObject implements Serializable {
private String taskId = null;
private Double version = new Double(0.0);
private String assignee = null;
private String phone = null;
private String email = null;
private String guid = null;
private String isGroup = null;
public PcTaskAssignee() {
}
public int hashCode(){
return new HashCodeBuilder()
.append(taskId)
.append(version)
.append(assignee)
.toHashCode();
}
public boolean equals(AssigneeCompare compare){
if(this.taskId.equals(compare.getId())
//&&(this.version.equals(compare.getVersion()) )
&&(this.assignee.equals(compare.getAssignee()) ) )
return true;
else
return false;
}
public String getName(){
return assignee;
}
public void setAssignee(String assignee) {
this.assignee = assignee;
}
public String getAssignee() {
return assignee;
}
public void setGuid(String guid) {
this.guid = guid;
}
public String getGuid() {
return guid;
}
public void setIsGroup(String isGroup) {
this.isGroup = isGroup;
}
public String getIsGroup() {
return isGroup;
}
public boolean sonoPiuAssegnatariDelGruppo(String groupId) {
if ( (isGroup.equalsIgnoreCase("F")) ||
(isGroup==null) ||
(this.guid.equals(groupId)) ){
return false;
} else {
return true;
}
}
public boolean sonoPiuAssegnatari() {
if (isGroup==null || isGroup.equalsIgnoreCase("F")){
return false;
} else {
return true;
}
}
public void setTaskId(String taskId) {
this.taskId = taskId;
}
public String getTaskId() {
return taskId;
}
public void setVersion(Double version) {
this.version = version;
}
public Double getVersion() {
return version;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPhone() {
return phone;}
public void setEmail(String email) {
this.email = email;}
public String getEmail() {
return email;}
}
and mapped as follows :
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="previtc.model.task">
<class name="PcTaskAssignee" table="PC_TASKASSIGNEE" dynamic-update="true" dynamic-insert="true">
<composite-id>
<key-property name="taskId" type="string" column="TASKID" />
<key-property name="version" type="double" column="VERSION" />
<key-property name="assignee" type="string" column="ASSIGNEE"/>
</composite-id>
<property name="phone" type="string" column="PHONE" />
<property name="email" type="string" column="EMAIL" />
<property name="guid" type="string" column="GUID" />
<property name="isGroup" type="string" column="ISGROUP"/>
</class>
</hibernate-mapping>
I wish to retrieve a single instance of PcTaskAssignee having taskId = xx version = yy and assignee = zz , so I wrote the following
PcTaskAssignee assignatario = (PcTaskAssignee)getSession().get(PcTaskAssignee.class,compare);
where compare is a comparing object, inizialized with the values xx,yy,zz which I want to correspond in PcTaskAssignee. compare is defined as follows
Code:
package previtc.model;
import java.io.Serializable;
public class AssigneeCompare implements Serializable{
public AssigneeCompare() {
}
public AssigneeCompare(String taskId,Double version) {
this.id = taskId;
this.version = new Double(version.doubleValue());
}
String id;
String assignee;
Double version;[... getters/setters here ...]
but I'm getting exception
exception setting property value with CGLIB (set
hibernate.cglib.use_reflection_optimizer=false for more info) setter of previtc.model.task.PcTaskAssignee. ?I just want to understand how to use the method equals() and hashCode() to implement a filtering criteria which I explained
above. Could somebody explain me this ?
Thanks a lot, Renato.
P.S. I am using Oracle Database , and my PcTaskAssignee corresponding table is defined as follows :
CREATE TABLE PC_TASKASSIGNEE
(
TASKID NVARCHAR2(32),
VERSION NUMBER,
ASSIGNEE NVARCHAR2(200),
GUID NVARCHAR2(32),
ISGROUP NVARCHAR2(2),
PHONE NVARCHAR2(100),
EMAIL NVARCHAR2(100)
)I read on the internet that this problem is usually caused by attempting to set a null value to a row / field / attribute which is defined as a primitive ( for example trying to set boolean b = null )