I have a one to many relation entities Scenario is like this EmployeeGroupActivity has Many EmployeeActivity Hibernate Mapping is done as follows
@Entity
@Table(name = “employeegroupactivity”)
@DynamicUpdate
public class EmployeeGroupActivity{
/**
*
*/
private static final long serialVersionUID = -9114620718714111316L;
private Integer groupActivityId;
private Set<EmployeeActivity> employeeActivities;
public EmployeeGroupActivity(){
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "groupActivityId")
public Integer getGroupActivityId() {
return groupActivityId;
}
public void setGroupActivityId(Integer groupActivityId) {
this.groupActivityId = groupActivityId;
}
@OneToMany(targetEntity = EmployeeActivity.class,mappedBy = "groupEmployeeActivity",fetch = FetchType.LAZY)
public Set<EmployeeActivity> getEmployeeActivities() {
return employeeActivities;
}
public void setEmployeeActivities(Set<EmployeeActivity> employeeActivities) {
this.employeeActivities = employeeActivities;
}
}
// EmployeeActivity
@Entity
@Table(name = "employeeactivity")
@DynamicUpdate
public class EmployeeActivity{
private Integer empActivityId;
String activityDescription;
Date activityDate;
private Integer eventId;
private EmployeeGroupActivity groupEmployeeActivity; //This is mapped as ManyToOne
}
// Query Part
Criterion eventIdCriterion = Restrictions.eq(“EG.eventId", eventId);
Projection projection = Projections.projectionList()
.add(Projections.property(“EG.groupActivityId"),"groupActivityId")
.add(Projections.property("EMPACT.empActivityId”), "employeeActivities.empActivityId")
.add(Projections.property("EMPACT.activityDescription”), "employeeActivities.activityDescription")
.add(Projections.property("EMPACT.activityDate"), "employeeActivities.activityDate");
Criteria criteria = sessionObject.createCriteria(EmployeeGroupActivity.class,”EG")
.createAlias(“EG.employeeActivities", “EMPACT”)
.setProjection(projection)
.add(eventIdCriterion);
criteria
.setResultTransformer(new AliasToBeanNestedResultTransformer(
GroupActivityResponse.class));
When I fetch EmployeeGroupActivity with a where clause I am expecting one EmployeeGroupActivity for an event which has many EmployeeActivity objects.
But I am getting result as Many EmployeeGroupActivity objects all with same groupActivityId but each object has one EmployeeActivity object in a set of EmployeeActivity.
This happens only when I use projections with hibernate criteria, If I don't use projections I will get one EmployeeGroupActivity object which has many EmployeeActivity objects.
I am using setResultTransformer for criteria and a library http://stackoverflow.com/questions/20331852/java-hibernate-transformer-aliastobeannestedresulttransformer posted by some in in SO
How to get Set of employee activities with projections ? I have many such requirement and every where I have to iterate the result and make one root object add all set items then return in response which takes lot of iterations.
Update : note that DISTINCT_ROOT_ENTITY didn't work
This is a known issue for Hibernate criteria. A work around to eliminate duplicates is to get the distinct id's matching your constraints by
and then you can create another criteria to get details of the returned id's
Hope this works.