Hibernate 映射和继承

映射关系:

Many-to-one单向

Person – department

Person:

public class Person implements Serializable{
private String pno;
private String name;
private String address;
private DepartmentEntity dept;
private int age;
}


Person.hbm.xml :

<class name="com.etc.entity.doubleface.Person" table="t_mtoo3_person">
<id name="pno"><generator class="uuid"/></id>
<property name="name"/>
<property name="address"/>
<property name="age"/>
<many-to-one name="dept" column="deptid"></many-to-one>
</class>


 

DepartmentEntity:

public class DepartmentEntity implements Serializable{
private static final long serialVersionUID = 1L;
private String deptno;
private String deptname;
private String deptaddress;
}


DepartmentEntity.hbm.xml:

<class name="com.etc.entity.doubleface.DepartmentEntity" table="t_mtoo3_dept">
<id name="deptno"><generator class="uuid"/></id>
<property name="deptname"/>
<property name="deptaddress"/>
</class>

 


Many-to-one双向 One-to-many:(去掉任意一边关系配置就成单向)

多个Person对一个 department  

一个department对多个Person

Person:

public class Person implements Serializable{
private String pno;
private String name;
private String address;
private DepartmentEntity dept;
private int age;
}

Person.hbm.xml:

<class name="com.etc.entity.doubleface.Person" table="t_mtoo3_person">
<id name="pno"><generator class="uuid"/></id>
<property name="name"/>
<property name="address"/>
<property name="age"/>
<many-to-one name="dept" column="deptid"></many-to-one>
</class>


DepartmentEntity:

public class DepartmentEntity implements Serializable{
private static final long serialVersionUID = 1L;
private String deptno;
private String deptname;
private String deptaddress;
private Set persons;
}


DepartmentEntity.hbm.xml:

<class name="com.etc.entity.doubleface.DepartmentEntity" table="t_mtoo3_dept">
<id name="deptno"><generator class="uuid"/></id>
<property name="deptname"/>
<property name="deptaddress"/>
<set name="persons">
<key column="deptid"></key>
<one-to-many class="com.etc.entity.doubleface.Person"/>
</set>
</class>


One-to-one

一个学生对应一个学号(主键双向关联)

Student- StudentNo

Student:

public class Student {
private String id;
private StudentNo stuno;
private String name;
private int age;
}


Student.hbm.xml:

<class name="com.etc.entity.onetoone.Student" table="t_otoo_student">
 <id name="id">
        <generator class="foreign">
            	<param name="property">stuno</param>
        	</generator>
    </id>
<property name="name"/>
<property name="age"/>
<one-to-one name="stuno" constrained="true"></one-to-one>
</class>


 

StudentNo:

public class StudentNo {
private String sno;
private String mem;
private Student stu;
}


StudentNo.hbm.xml:

<class name="com.etc.entity.onetoone.StudentNo" table="t_otoo_stuno">
<id name="sno"><generator class="uuid"/></id>
<property name="mem"/>
<one-to-one name="stu"/>
</class>



One-to-one

一个学生对应一个学号(外键双向关联)

Student- StudentNo

Student:

public class Student {
private String id;
private StudentNo stuno;
private String name;
private int age;
}


 Student.hbm.xml:

<class name="com.etc.entity.onetoonefk.Student" table="t_otoofk_student">
 <id name="id">
        <generator class="uuid">
        	</generator>
    </id>
<property name="name"/>
<property name="age"/>
<many-to-one name="stuno" 
       	    column="stunoid" 
        	unique="true"
        	not-null="true" cascade="save-update" />
</class>


 

StudentNo:

public class StudentNo {
private String sno;
private String mem;
private Student stu;
}


StudentNo.hbm.xml:

<class name="com.etc.entity.onetoonefk.StudentNo" table="t_otoofk_stuno">
<id name="sno">
<generator class="uuid"/>
</id>
<property name="mem"/>
<one-to-one name="stu"  property-ref="stuno"/>
</class>



Many-to-many 双向关联

Course- Student(一门课程可以多个学生学习,一个学生学习多门课程)

Course:

public class Course {
private String cno;
private String coursemem;
private String coursename;
private Set students;}


Course.hbm.xml:

<class name="com.etc.entity.manytomany.Course" table="t_mtom1_course">
<id name="cno"><generator class="uuid"/></id>
<property name="coursemem"/>
<property name="coursename"/>
<set name="students" table="t_mtom_student_course">
<key column="courseid"></key>
<many-to-many class="com.etc.entity.manytomany.Student" column="stuid"/>
</set></class>


Student:

public class Student {
private String id;
private Set courses;
private String name;
private int age;
}


Student.hbm.xml:

<class name="com.etc.entity.manytomany.Student" table="t_mtom1_student">
 <id name="id"><generator class="uuid"></generator></id>
<property name="name"/>
<property name="age"/>
<!--  name属性采用实体中变量名称;table属性表示生成的中间表表名;cascade属性表示级联 操作    -->
<set name="courses" table="t_mtom_student_course" cascade="save-update">
<key column="stuid"></key><!-- 将此表的主键拿到中间表中的列名为 stuid -->
<many-to-many class="com.etc.entity.manytomany.Course" column="courseid"/>
<!-- com.etc.entity.manytomany.Course对应表的主键拿到中间表中的列名为 courseid -->
</set>
</class>



hibernate继承

Student继承PersonTeacher继承Person

Person

public class Person {
private  String pno;
private String name;
private int age;
}
Student:
public class Student  extends Person{
private String school;
}
Teacher:
public class Teacher extends Person{
private String money;
}


一. 生成一张表:

Person.hbm.xml:

<class name="com.etc.entity.Person" table="t_extends_person">
 <id name="pno">
        <generator class="uuid">
        	</generator>
    </id>
    <discriminator column="Person_TYPE" type="string"/><!-- 鉴别器 -->
<property name="name"/>
<property name="age"/>
   	  <subclass name="com.etc.entity.Student" discriminator-value="S">
        <property name="school" />
    </subclass>
    <subclass name="com.etc.entity.Teacher" discriminator-value="T">
        <property name="money" />
    </subclass>
</class>


 


二.生成两张表

<class name="com.etc.entity.Person">
 <id name="pno">
        <generator class="uuid">
        	</generator>
    </id>
<property name="name"/>
<property name="age"/>
<union-subclass name="com.etc.entity.Student" table="t_extends2_student">
        	<property name="school"/>
    </union-subclass>
    <union-subclass name="com.etc.entity.Teacher" table="t_extends2_teacher">
        <property name="money"/>
    </union-subclass>
</class>


数据库表记录:

t_extends2_teacher

 

t_extends2_student

 


三.生成三张表

<class name="com.etc.entity.Person" table="t_extends3_person">
 <id name="pno">
        <generator class="uuid">
        	</generator>
    </id>
<property name="name"/>
<property name="age"/>
    <joined-subclass name="com.etc.entity.Student" table="t_extends3_student">
        <key column="pno"/>
        <property name="school"/>
    </joined-subclass>
   <joined-subclass name="com.etc.entity.Teacher" table="t_extends3_teacher">
        <key column="pno"/>
        <property name="money"/>
    </joined-subclass>
</class>


表记录:

t_extends3_person

 

t_extends3_student

 

t_extends3_teacher

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值