Hibernate标准。按别名选择

时间:2014-04-01 04:27:51

标签: java hibernate

我有桌子

@Entity
@Table(name="cash")
public class CashItem extends Item {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "cash_seq_gen")
    @SequenceGenerator(name = "cash_seq_gen", sequenceName = "cash_seq")
    private Long id;

    @Column(name="object_id")
    private Long objectId = 1l;

    @Column(name="object_type")
    private Long objectType = 1l;

和链接表

@Entity
@Table(name="cash_to_order")
@SuppressWarnings("serial")
public class CashToOrderItem extends Item {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "cash_to_order_seq_gen")
    @SequenceGenerator(name = "cash_to_order_seq_gen", sequenceName = "cash_to_order_seq")
    Long id;

    @ManyToOne
    @JoinColumn(name="order_id",referencedColumnName="id")
    private OrderItem order;

    @ManyToOne
    @JoinColumn(name="cash_id",referencedColumnName="id")
    private CashItem cash;

我从CashToOrderItem获取Criteria并想进行下一次查询

criteria.createAlias("cash","c1");
criteria.add(Restrictions.eq("c1.objectType",ocf.getCounterparty()));
criteria.add(Restrictions.eq("с1.objectId",ocf.getSubCounterparty()));

但是 无法解析属性的错误:с1of:CashToOrderItem 如果我添加了两个或更多限制。

1 个答案:

答案 0 :(得分:0)

如果您要在CashToOrder上创建条件,请从该类别中获取别名现金。即cashToOrder.cash

Criteria criteria = session.createCriteria(CashToOrderItem.class, "cashToOrder");
criteria.createAlias("cashToOrder.cash","c1");