2013-06-18 45 views
0

只有一個Envelope對象和2個發票對象被這個Envelope映射。當我嘗試用下面的代碼查詢時,它返回2個相同的Envelope對象。我認爲我的hibernate註釋存在問題。是否有解決方案?Hibernate Criteria查詢示例映射

Envelope envelope = new Envelope(); 
envelope.setPostBox(EnvelopePostBox.INBOX.name());   
List<Envelope> byTemplate = genericDao.getByTemplate(envelope); 

信封實體;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "envelope", fetch = FetchType.EAGER) 
private List<Invoice> invoiceList; 

發票實體;

@JoinColumn(name = "envelope", referencedColumnName = "instance_identifier") 
@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER) 
private Envelope envelope; 

我的道法;

@Transactional(readOnly = true) 
public <T> List<T> getByTemplate(T templateEntity) { 
    Criteria criteria = getCurrentSession().createCriteria(templateEntity.getClass()); 
    criteria.add(Example.create(templateEntity)); 
    return criteria.list(); 
} 

回答

0

嘗試下面的代碼,

@Transactional(readOnly = true) 
public <T> List<T> getByTemplate(T templateEntity) { 
    Criteria criteria = getCurrentSession().createCriteria(templateEntity.getClass()); 
    criteria.add(Example.create(templateEntity)); 
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); 
    return criteria.list(); 
} 
+0

不工作。 –

+0

你可以嘗試刪除或註釋criteria.add(Example.create(templateEntity)); –