0

此信封映射的只有一个信封对象和两个发票对象。当我尝试使用以下代码查询时,它返回两个相同的信封对象。我认为我的休眠注释有问题。有什么解决办法吗?

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();
}
4

1 回答 1

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();
}
于 2013-06-18T12:29:26.717 回答