简体   繁体   English

在Hibernate中如何在多对多关联中使用索引列?

[英]How to use index column in many-to-many association in criteria in Hibernate?

I'm using Hibernate 3 with Java. 我正在Java中使用Hibernate 3。

My POJOs classes are as follows: 我的POJO类别如下:

Teacher.java 教师

private long id;
private String teacherName;
private List<Student> students;
// getter-setter of all

Student.java 学生.java

 private long id;
 private String studentName;

// getter-setter of both

Teacher.hbm.xml Teacher.hbm.xml

<class name="Teacher" table="teacher_master">
    <id name="id" column="id" type="long">
        <generator class="native"></generator>
    </id>

    <property column="teacher_name" name="teacherName" type="string" />

    <list name="students" cascade="refresh">
        <key column="teacher_id"/>
        <index column="student_position" type="integer"/>
        <one-to-many class="Student"/>
    </list>
</class>

Student.hbm.xml contains mappings for id & studentName properties. Student.hbm.xml包含idstudentName属性的映射。

My database structure looks like the following: 我的数据库结构如下所示:

teacher_master Teacher_master

id  | teacher_name
----|--------------
1   | teacher1
2   | teacher2

student_master student_master

id  | student_name  | teacher_id  | student_position
----|---------------|-------------|------------------
1   | student1      |      1      |      0
2   | student2      |      1      |      2
3   | student3      |      1      |      1

Now I want to fetch all Students attached to Teacher with id = 1, in the order of student_position . 现在,我要按Student_position的顺序获取ID = 1的所有附加到Teacher的学生。

I've written the following criteria: 我写了以下标准:

List<Long> ids = new ArrayList<Long>();
ids.add(1l);
ids.add(2l);
ids.add(3l);
Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.in("id", ids));
List<Student> students = criteria.list();

Here students will give me the records in order of the primary key that is 1,2,3. 在这里, 学生将按主键1,2,3的顺序给我记录。

I want these records to be in order of their student_position that is 1,3,2. 我希望这些记录按其Student_position为1,3,2的顺序排列。

How can I achieve this? 我该如何实现?

     Long techId=1l;
     Criteria criteria = getSession().createCriteria(Student.class, "student");
     criteria.add(Restrictions.eq("student.teacherId", techId));
     criteria.addOrder(Order.asc("student.studentPosition"));

You cant query for object with criteria If the field is not present in the model object. 如果模型对象中不存在该字段,则无法使用条件查询对象。 The Other way you can go with is hibernate native query. 您可以使用的其他方法是休眠本机查询。

    Long techId=1l;
    Query query = session.createSQLQuery(
           "select * from student_master s where s.teacher_id = :teacherId order by s.student_position")
            .addEntity(Student.class)
            .setParameter("teacherId", "techId");
             List result = query.list();

You can add an ordering criterion, org.hibernate.criterion.Order 您可以添加排序条件org.hibernate.criterion.Order

criteria.addOrder( Order.asc("yourPropertyName"));

You can also add more than one of these orderings. 您还可以添加多个以上订购之一。 Also see here under 15.3 另请参阅此处的 15.3

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

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