具体参考:
Team.java
package com.fgh.hibernate; import java.util.Set; public class Team { private String id; private String teamName; private Set<Student> 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<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } }
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; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.fgh.hibernate.Team" table="team"> <id name="id" column="id" type="string"> <generator class="uuid"></generator> </id> <property name="teamName" column="teamName" type="string"></property> <!-- 这里sort属性设置我们自己的排序规则 --> <set name="students" lazy="false" cascade="all" inverse="true" fetch="select" sort="com.fgh.hibernate.MyComparator"> <key column="team_id"></key> <one-to-many class="com.fgh.hibernate.Student" /> </set> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.fgh.hibernate.Student" table="student"> <id name="id" column="id" type="string"> <generator class="uuid"></generator> </id> <property name="name" column="name" type="string"></property> <property name="idCard" column="idcard" type="string"></property> <property name="age" column="age" type="integer"></property> <many-to-one name="team" class="com.fgh.hibernate.Team" column="team_id"> </many-to-one> </class> </hibernate-mapping>
自定义排序规则类:
MyComparator.java
package com.fgh.hibernate; import java.util.Comparator; /** * 自定义排序规则类 * * @author fgh * */ public class MyComparator implements Comparator<Student> { 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 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<Student> set = team2.getStudents(); for (Iterator<Student> 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