编程开发 购物 网址 游戏 小说 歌词 快照 开发 股票 美女 新闻 笑话 | 汉字 软件 日历 阅读 下载 图书馆 编程 China
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
移动开发 架构设计 编程语言 互联网 开发经验 Web前端 开发总结
开发杂谈 系统运维 研发管理 数据库 云 计 算 Java开发
VC(MFC) Delphi VB C++(C语言) C++ Builder 其它开发语言 云计算 Java开发 .Net开发 IOS开发 Android开发 PHP语言 JavaScript
ASP语言 HTML(CSS) HTML5 Apache MSSQL数据库 Oracle数据库 PowerBuilder Informatica 其它数据库 硬件及嵌入式开发 Linux开发资料
  编程开发知识库 -> 互联网 -> Hibernate继承映射简单入门 -> 正文阅读
 

[互联网]Hibernate继承映射简单入门[第1页]

首先,映射继承分三种
1。一个类的继承树一个表
基于xml
Teacher类

public class Teacher {
    private Integer id;
    private String name;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }



}

子类Class_Teacher

public class Class_Teacher extends Teacher{
    private String classId;
    private Integer classNumber; 
    private Integer ground;



    public Class_Teacher() {
        super();
    }
    public Class_Teacher(String classId, Integer classNumber, Integer ground) {
        super();
        this.classId = classId;
        this.classNumber = classNumber;
        this.ground = ground;
    }
    public String getClassId() {
        return classId;
    }
    public void setClassId(String classId) {
        this.classId = classId;
    }
    public Integer getClassNumber() {
        return classNumber;
    }
    public void setClassNumber(Integer classNumber) {
        this.classNumber = classNumber;
    }
    public Integer getGround() {
        return ground;
    }
    public void setGround(Integer ground) {
        this.ground = ground;
    }


}

子类Info_Teacher

public class Info_Teacher extends Teacher{
    private String weight;
    private String height;
    public String getWeight() {
        return weight;
    }
    public void setWeight(String weight) {
        this.weight = weight;
    }
    public String getHeight() {
        return height;
    }
    public void setHeight(String height) {
        this.height = height;
    }


}

teacher.hbm.xml

<?xml version='1.0' encoding='UTF-8'?>  
<!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.north.java.Hibernate.Teacher.Teacher" table="Teacher">  
    <id name="id" type="java.lang.Integer">
        <column name="ID" />
        <generator class="native" />
    </id>
    <discriminator column="type" type="string"/>  
    <property name="Name"></property>  

    <subclass name="com.north.java.Hibernate.Teacher.Class_Teacher"
     discriminator-value="class_teacher">
        <property name="classId"></property>
        <property name="classNumber"></property>
        <property name="ground"></property>
    </subclass>

    <subclass name="com.north.java.Hibernate.Teacher.Info_Teacher"
     discriminator-value="info_teacher">
        <property name="weight"></property>
        <property name="height"></property>
    </subclass>

  </class>  

 </hibernate-mapping>

在hibernate的配置文件添加
<mapping resource="com/north/java/Hibernate/Teacher/Teacher1.hbm.xml"/>
创建和储存

public class TestTeacher {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        TestTeacher tt = new TestTeacher();

        Class_Teacher ct =  new Class_Teacher("2,3", 2, 3);
        ct.setName("BB");

        Info_Teacher it = new Info_Teacher();
        it.setHeight("170");
        it.setWeight("100");
        it.setName("CC");

        tt.CreateTeacher("AA", ct, it);

        HibernateUtil.getSessionFactory().close();
    }


    public void CreateTeacher(String name, Class_Teacher ct, Info_Teacher it) {
        //HibernateUtil是工具类,返回SessionFactory对象
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        Teacher teacher = new Teacher();
        teacher.setName(name);

        session.save(teacher);
        session.save(ct);
        session.save(it);

        session.getTransaction().commit();

    }
}

结果

基于注解
Teacher类

@Entity
@Table(name="TeacherAnnoation")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type",discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue(value="teacher")
public class Teacher {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id")
    private Integer id;
    @Column(name="name")
    private String name;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }



}

Class_Teacher类

加上
@Entity
@DiscriminatorValue("class_taecher")

Info_Teacher类

加上
@Entity
@DiscriminatorValue("info_teacher")


hibernate的配置文件中
添加

<mapping class="com.north.java.Hibernate.Teacher.Teacher"></mapping>
        <mapping class="com.north.java.Hibernate.Teacher.Class_Teacher"></mapping>
        <mapping class="com.north.java.Hibernate.Teacher.Info_Teacher"></mapping>

结果

2.每一个字类一个表
基于xml
teacher.hbm.xml文件

<?xml version='1.0' encoding='UTF-8'?>  
<!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.north.java.Hibernate.Teacher.Teacher" table="Teacher2">  
    <id name="id" type="java.lang.Integer">
        <column name="ID" />
        <generator class="native" />
    </id>

    <property name="Name"></property>  

    <joined-subclass name="com.north.java.Hibernate.Teacher.Class_Teacher"
     table="class_teacher2">
        <key column="cid"/> 
        <property name="classId"></property>
        <property name="classNumber"></property>
        <property name="ground"></property>
    </joined-subclass>

    <joined-subclass name="com.north.java.Hibernate.Teacher.Info_Teacher"
     table="info_teacher2">
         <key column="iid"/> 
        <property name="weight"></property>
        <property name="height"></property>
    </joined-subclass>

  </class>  

 </hibernate-mapping>

结果
基于注解
teacher类
加上

@Entity
@Table(name="TeacherAnnoation1")
@Inheritance(strategy=InheritanceType.JOINED)

class_Teacher类
加上

@Entity
@Table(name="class_table_annotion1")
@PrimaryKeyJoinColumn(name="ID")

info_Teacher类
加上

@Entity
@Table(name="info_table_annotion1")
@PrimaryKeyJoinColumn(name="ID")

结果
TeacherAnnoation1表

class_table_annotion1表

info_table_annotion1表

3.每个类一张表
基于xml
Teacher.hbm.xml

<?xml version='1.0' encoding='UTF-8'?>  
<!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.north.java.Hibernate.Teacher.Teacher" table="Teacher1">  
    <id name="id" type="java.lang.Integer">
        <column name="ID" />
        <generator class="assigned" />
    </id>

    <property name="Name"></property>  

    <union-subclass name="com.north.java.Hibernate.Teacher.Class_Teacher"
     table="class_teacher1">
        <property name="classId"></property>
        <property name="classNumber"></property>
        <property name="ground"></property>
    </union-subclass>

    <union-subclass name="com.north.java.Hibernate.Teacher.Info_Teacher"
     table="info_teacher1">
        <property name="weight"></property>
        <property name="height"></property>
    </union-subclass>

  </class>  

 </hibernate-mapping>

注意这个的id是要自己添加的
class_teacher表

info_teacher表

基于注解
teacher类加上

@Entity
@Table(name="TeacherAnnoation2")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)

Class_teacher类

@Entity
@Table(name="class_table_annotion2")
@AttributeOverrides({
    @AttributeOverride(name = "id", column = @Column(name = "id")),
    @AttributeOverride(name = "name", column = @Column(name = "name")) })

info_Teacher类

@Entity
@Table(name="info_table_annotion2")
@AttributeOverrides({
    @AttributeOverride(name = "id", column = @Column(name = "id")),
    @AttributeOverride(name = "name", column = @Column(name = "name")) })

结果
TeacherAnnoation2表

class_table_annotion2表

info_table_annotion2表

  互联网 最新文章
Stanford 英文词性标注(Part-of-speech)缩
基于窗口的实时统计
求解矩阵最短路径问题
SSL握手通信详解及linux下c/c++ SSL Socket
关于服务器上(Docker中)运行Java程序时区
python爬虫系列(六):强大的beautifulsou
[计算机网络笔记]第四部分——网络层 选路算
11.28 北京,念腾讯暑假,不思则惘吧!
web安全之
滑块验证码识别 java版本
上一篇文章      下一篇文章      查看所有文章
加:2017-09-28 16:13:45  更:2017-09-28 16:14:08 
VC(MFC) Delphi VB C++(C语言) C++ Builder 其它开发语言 云计算 Java开发 .Net开发 IOS开发 Android开发 PHP语言 JavaScript
ASP语言 HTML(CSS) HTML5 Apache MSSQL数据库 Oracle数据库 PowerBuilder Informatica 其它数据库 硬件及嵌入式开发 Linux开发资料
360图书馆 软件开发资料 文字转语音 购物精选 软件下载 新闻资讯 小游戏 Chinese Culture 股票 三丰软件 开发 中国文化 网文精选 阅读网 看图 日历 万年历 2018年11日历
2018-11-13 1:38:43
多播视频美女直播
↓电视,电影,美女直播,迅雷资源↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  编程开发知识库