标准查询多对一单向关系

时间:2015-05-12 13:06:46

标签: java sql jpa criteria-api hibernate-criteria

question回答了相反的关系。我一直试图为以下模型提出Criteria Query:

@Entity
public class One {

    @Id
    private BigInteger supplierId;

    @Column(name = "name")
    String name;


    ...
}

@Entity
public class Many {

    @Id
    private BigInteger posId;

    @ManyToOne
    @JoinColumn(name = "column_name")
    One one;

    @Column(name = "description")
    String description;
}

我无法改变模型。我最常以这种方式保持单向。我正在尝试针对以下情况提出Criteria Builder代码: - 我将获得以下属性:namdesc。我需要找到名称属性等于One的所有nam实体,并且这些实体具有相关的Many实体,其描述属性等于desc

1 个答案:

答案 0 :(得分:-2)

Criteria query = this.getSession().createCriteria(Many.class);
query = query.add(Restrictions.eq("one.name", nam));
query = query.add(Restrictions.eq("description", desc));
query.list();

这种方式不起作用?