简体   繁体   中英

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 . 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

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??

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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