Hibernate内存排序

内存排序
内存排序和数据库排序唯一的不同就是在配置文件中使用sort属性而不是order-by属性
他有三个属性值(unsorted,natural),其中natural指的是按照自然的升序排序,第三个
属性值是我们自己定义 的排序规则类,该规则类通过实现Comparator接口,并且实现
该接口中的Compare方法,在该方法中定义排序规则即可。然后将该自定义的排序规则
的类名作为sort的属性值即可.

具体参考:

Team.java

 

package com.fgh.hibernate; import java.util.Set; public class Team { private String id; private String teamName; private Set students; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTeamName() { return teamName; } public void setTeamName(String teamName) { this.teamName = teamName; } public Set getStudents() { return students; } public void setStudents(Set students) { this.students = students; } }
Student.java

 

 

package com.fgh.hibernate; /** * 一对一双向关联 * * @author fgh * */ public class Student { private String id; private String name; private String idCard; private int age; private Team team; public Team getTeam() { return team; } public void setTeam(Team team) { this.team = team; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getIdCard() { return idCard; } public void setIdCard(String idCard) { this.idCard = idCard; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Team.hbm.xml

 

 


Student.hbm.xml

 

 

自定义排序规则类:

MyComparator.java

 

package com.fgh.hibernate; import java.util.Comparator; /** * 自定义排序规则类 * * @author fgh * */ public class MyComparator implements Comparator { public int compare(Student s1, Student s2) { //如果两个对象相同,不排序 if (s1 == s2) { return 0; } int result = s1.getIdCard().compareToIgnoreCase(s2.getIdCard()); //如果cardId不同,按照cardId排序 if (result != 0) { //如果降序 return -result; return result; } //如果cardId相同,按照name排序 //如果降序 返回负值 return s1.getName().compareToIgnoreCase(s2.getName()); } }
测试类:

 

HIbernateTest.java

 

package com.fgh.hibernate; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class HibernateTest { private static SessionFactory sessionFactory; static { try { sessionFactory = new Configuration().configure() .buildSessionFactory(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { // 先执行插入操作 有了数据才可以查询哦 // ------------begin-------------- Team team = new Team(); team.setTeamName("team1"); team.setStudents(new HashSet()); Student student = new Student(); student.setName("zhangsan"); student.setIdCard("aa"); student.setAge(20); Student student1 = new Student(); student1.setName("lisi"); student1.setIdCard("aa"); student1.setAge(21); Student student2 = new Student(); student2.setName("wangwu"); student2.setIdCard("ba"); student2.setAge(22); Student student3 = new Student(); student3.setName("xiaoqiang"); student3.setIdCard("bb"); student3.setAge(23); Student student4 = new Student(); student4.setName("xiaohong"); student4.setIdCard("dc"); student4.setAge(24); team.getStudents().add(student); team.getStudents().add(student1); team.getStudents().add(student2); team.getStudents().add(student3); team.getStudents().add(student4); student.setTeam(team); student1.setTeam(team); student2.setTeam(team); student3.setTeam(team); student4.setTeam(team); // -------------end------------ Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); // 查询----bein-------- Team team2 = (Team) session.get(Team.class, "8a8ae4d934b2bfa10134b2bfa4a30001"); Set set = team2.getStudents(); for (Iterator iterator = set.iterator(); iterator .hasNext();) { System.out.println(iterator.next().getName()); } // --------------end---------- // 保存 // session.save(team); tx.commit(); } catch (Exception e) { e.printStackTrace(); if (null != tx) { tx.rollback(); } } finally { session.close(); } } } 打印sql:

 

 

Hibernate: select team0_.id as id1_0_, team0_.teamName as teamName1_0_ from team team0_ where team0_.id=? Hibernate: select students0_.team_id as team5_1_, students0_.id as id1_, students0_.id as id0_0_, students0_.name as name0_0_, students0_.idcard as idcard0_0_, students0_.age as age0_0_, students0_.team_id as team5_0_0_ from student students0_ where students0_.team_id=? lisi zhangsan wangwu xiaoqiang xiaohong
哦也,完成

 

 

 

你可能感兴趣的:(Hibernate,hibernate,内存排序)