通过其中一个复合主键查找记录

时间:2021-08-07 02:09:27

I have composite primary key in one of my tables in Java web app using Hibernate framework:

我使用Hibernate框架在Java web app中的一个表中有复合主键:

For example:

@Embeddable
public class Student implements Serializable {    
    @Column(name = "student_id")
    private String studentId;

    @Column(name = "course_code")
    private String courseCode;
}

I can filter records by both fields. However, I want to search for a record with studentId only. How would it be possible in Hibernate?

我可以按两个字段过滤记录。但是,我想仅使用studentId搜索记录。怎么可能在Hibernate中?

1 个解决方案

#1


Try following:

Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.eq("student_id", student_id));
criteria.add(Restrictions.eq("course_code", course_code));
List list = criteria.list();

#1


Try following:

Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.eq("student_id", student_id));
criteria.add(Restrictions.eq("course_code", course_code));
List list = criteria.list();