Hibernate一对多返回多重复制对象

时间:2015-07-22 02:49:13

标签: java hibernate nhibernate-projections

我有一对多关系实体 场景就是这样 EmployeeGroupActivity具有许多EmployeeActivity Hibernate Mapping按如下方式完成

@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));

当我使用where子句获取EmployeeGroupActivity时,我期望一个EmployeeGroupActivity用于具有许多EmployeeActivity对象的事件。

但我得到了结果 许多EmployeeGroupActivity对象都具有相同的groupActivityId,但每个对象在一组EmployeeActivity中都有一个EmployeeActivity对象。

仅当我使用具有休眠标准的投影时才会发生这种情况。如果我不使用投影,我将获得一个具有许多EmployeeActivity对象的EmployeeGroupActivity对象。

我正在使用setResultTransformer作为标准,而某些人在SO中发布了一个库http://stackoverflow.com/questions/20331852/java-hibernate-transformer-aliastobeannestedresulttransformer

如何通过预测获得员工活动集?我有很多这样的要求,每一个我必须迭代结果,并使一个根对象添加所有设置项然后返回响应,这需要大量的迭代。

更新:请注意DISTINCT_ROOT_ENTITY无效

1 个答案:

答案 0 :(得分:0)

这是Hibernate标准的已知问题。 努力消除重复的工作是 通过

获得与您的约束匹配的独特ID
criteria.setProjection(Projections.distinct(Projections.id()));
idList = criteria.list();

然后您可以创建另一个条件来获取返回的ID的详细信息

criteria2.add(Restrictions.in("id", idList));
requiredList = cr.list();

希望这有效。