0

我在休眠条件查询中遇到问题我的 Pojo 映射如下

@Entity
@Table(name = "wsc_taheader")
public class TraceableActivityHeader{
@OneToMany(cascade = { CascadeType.ALL }, fetch  = FetchType.EAGER)
@JoinColumn(name = "taid")
@BatchSize(size=15)
private List<TraceableActivityStep> steps = new ArrayList<TraceableActivityStep>();
@Id
@Column(name = "taid")
public String taId;

}

@Entity
@Table(name = "wsc_tastep")
public class TraceableActivityStep implements BindObject {

private static final long serialVersionUID = 1L;

@ManyToOne
@JoinColumn(name = "taid", insertable = false, updatable = false, nullable = false)
private TraceableActivityHeader taHeader;
@Id
@GeneratedValue
@Column(name = "stepid")
private long stepId;
}

在上面映射我在数据库中的数据就像

wsc_taheader
taId
1
2

wsc_tastep
stepId taId stepName
 1      1   BalanceQuery
 2      2   FundTransfer 1
 3      2   FundTransfer 2

在上面的映射中,我创建了条件查询

public    List<TraceableActivityHeader>searchTAHeaderByExampleCriteria(TraceableActivityHeader exampleTAHeader) {
    List<Criterion> externalCriteria=new ArrayList<Criterion>();
    Criteria criteria = getCriteriaForExampleSearch(exampleTAHeader,  externalCriteria);
    return criteria.list();
    }
protected Criteria getCriteriaForExampleSearch(Object exampleObject,
        List externalCriteria) {
    Example example = Example.create(exampleObject).enableLike(
            MatchMode.ANYWHERE).ignoreCase();
    return getCriteriaForExampleSearch(example, exampleObject.getClass(),
            externalCriteria, new DefaultPropertySelector());
}
protected Criteria getCriteriaForExampleSearch(Example example,
        Class exampleClass, List<Criterion> externalCriteria,
        PropertySelector propertySelector) {

    Criteria criteria = getSession().createCriteria(exampleClass);
    criteria.add(example);

    criteria.add(example.setPropertySelector(propertySelector));
    if (externalCriteria != null && !externalCriteria.isEmpty()) {
        for (Iterator<Criterion> iter = externalCriteria.iterator(); iter.hasNext();) {
            Criterion element = iter.next();
            criteria.add(element);
        }
    }
    return criteria;

}

但 searchTAHeaderByExampleCriteria() 返回的结果是

List<TraceableActivityHeader> list = traceableActivityManager
            .searchTAHeaderByExampleCriteria(traceableActivityHeader);
list.size is 3 which should be 2

数据就像

1. list.get(0) with TraceableActivityHeader
                taId=1
                    steps
                        taId=1 stepId=2
2. list.get(1) with TraceableActivityHeader
                taId=2
                    steps
                        taId=2 stepId=1
                        taId=2 stepId=2

3. list.get(2) with TraceableActivityHeader
                taId=2
                    steps
                        taId=2 stepId=1
                        taId=2 stepId=2
4

1 回答 1

0

通过设置处理查询结果的策略,我能够解决问题

criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
于 2013-04-26T13:33:53.923 回答