2012-12-07 149 views
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 

回答

0

通過設置策略來處理查詢結果我能解決這個問題

criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);