了解子标准hibernate

时间:2015-05-25 06:52:14

标签: java hibernate

假设我有以下映射:

@Entity
@Table(name = "dp")
public class DailyProfit {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @ManyToOne(targetEntity = Exchange.class, fetch = FetchType.EAGER)
    @JoinColumn(name="exchange_id")
    private Exchange exchange;
}

我想创建一个条件查询,以便它返回所有具有id > 100的Exchange的结果。我可以写

吗?
Criteria criteria = getSession().createCriteria(DailyProfit.class);
criteria.add(Restrictions.gt("exchange.id", 100));

而不是创建如下的子标准:

Criteria criteria = getSession().createCriteria(DailyProfit.class).createCriteria("exchange", "ex");
criteria.add(Restrictions.gt("ex.id", 100));

hibernate允许我们这样做吗?我尝试了这个,得到了一些看起来不正确的奇怪结果。它实际上做了什么?

0 个答案:

没有答案