【问题标题】:Hibernate Criteria query by Example mapping通过示例映射的 Hibernate Criteria 查询
【发布时间】:2013-06-18 11:38:59
【问题描述】:

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

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

【问题讨论】:

    标签: hibernate hibernate-mapping


    【解决方案1】:

    试试下面的代码,

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

    【讨论】:

    • 您可以尝试删除或评论 criteria.add(Example.create(templateEntity));
    猜你喜欢
    • 1970-01-01
    • 2013-08-15
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多