hibernate映射(学生-科目-成绩)

实体类

1 public class Student {
2     private int id;
3     private String name;
4     private Set<Score> scores = new HashSet<Score>();   //一对多
5 }
1 public class Score {
2     private int id;
3     private Student student;  //1对1
4     private Course course;      //1对1
5     private Double score;
6 }
1 public class Course {
2     private int id;
3     private String name;
4     private Set<Score> scores = new HashSet<Score>();    //一对多
5 }

方式一映射关系(多对一)有3个配置文件

 1 <hibernate-mapping package="entity">
 2     <class name="Course">
 3         <id name="id">
 4             <generator class="native"/>
 5         </id>
 6         <property name="name"/>
 7         <set name="scores" inverse="true">
 8             <key column="courseId"/>
 9             <one-to-many class="Score"/>
10         </set>
11     </class>
12 </hibernate-mapping>
 1 <hibernate-mapping package="entity">
 2     <class name="Score">
 3         <id name="id">
 4             <generator class="native"/>
 5         </id>
 6         <property name="score"/>
 7         <many-to-one name="student" column="studentId" not-null="true"/>
 8         <many-to-one name="course" column="courseId" not-null="true" lazy="false"/>
 9     </class>
10 </hibernate-mapping>
 1 <hibernate-mapping package="entity">
 2     <class name="Student">
 3         <id name="id">
 4             <generator class="native"/>
 5         </id>
 6         <property name="name"/>
 7 
 8         <set name="scores" inverse="true" lazy="false">
 9             <key column="studentId"/>
10             <one-to-many class="Score"/>
11         </set>
12     </class>
13 </hibernate-mapping>

方式二配置(只有两个文件)

 1 <hibernate-mapping package="entity">
 2     <class name="Student">
 3         <id name="id">
 4             <generator class="native"/>
 5         </id>
 6         <property name="name"/>
 7         <set name="scores" lazy="true" table="Score">
 8             <key column="studentId"/>
 9             <!-- 组成元素映射 -->
10             <composite-element class="Score">
11                 <!-- Score类中引用当前实体类的属性名 -->
12                 <parent name="student"/>
13                 <property name="score"/>
14                 <!-- Score类与Course类的多对一关联 -->
15                 <many-to-one name="course" class="Course" column="courseId"/>
16             </composite-element>
17         </set>
18     </class>
19 </hibernate-mapping>
 1 <hibernate-mapping package="entity">
 2     <class name="Course">
 3         <id name="id">
 4             <generator class="native"/>
 5         </id>
 6         <property name="name"/>
 7         <set name="scores" lazy="true" table="Score" inverse="true">
 8             <key column="courseId"/>
 9             <composite-element class="Score">
10                 <parent name="course"/>
11                 <property name="score"/>
12                 <many-to-one name="student" class="Student" column="studentId" foreign-key="studentId"/>
13             </composite-element>
14         </set>
15     </class>
16 </hibernate-mapping>

 

posted @ 2015-08-16 00:17  理舞  阅读(212)  评论(0编辑  收藏  举报