简体   繁体   English

多对一单向关系的条件查询

[英]Criteria query for many-to-one unidirectional relationship

This question answers the opposite relationship.这个问题回答了相反的关系。 I have been trying to come up with a Criteria Query for the following model:我一直在尝试为以下模型提出一个标准查询:

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

I cannot change the model.我不能改变模型。 I most keep it unidirectional in that way.我最喜欢以这种方式保持单向。 I am trying to come up with the Criteria Builder code for the following situation: -I will be given the following attributes: nam , desc .我正在尝试为以下情况提供 Criteria Builder 代码: -我将获得以下属性: namdesc I need to find all the One entities whose name attribute is equal to nam , and that have a related Many entity whose description attribute is equal to desc我需要找到 name 属性等于nam所有One实体,并且有一个相关的Many实体,其 description 属性等于desc

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

This way doesn't work??这个方法不行??

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM