二:Hibernate映射机制

来源:互联网 发布:c语言编程用什么软件 编辑:程序博客网 时间:2024/06/03 20:19



一:继承关系映射

student表



yjs(研究生)表



bks(本科生)表




student.java

public class Student{private Integer id;private String sno;private String name;private Date birth;//省略get和set方法}


Yjs.java

public class Yjs extends Student{private Integer yjsId;private String researchResult;//省略get和set方法}



Bks.java

public class Bks extends Student{private Integer bksId;private Boolean ky; //是否考研//省略get和set方法}



Xs.hbm.xml

<hibernate-mapping>    <class name="org.vo.Student" table="student" schema="dbo" catalog="TEST">        <id name="id" type="java.lang.Integer">            <column name="id" />            <generator class="native" />        </id>        <property name="sno" type="java.lang.String">            <column name="sno" length="50" />        </property>        <property name="name" type="java.lang.String">            <column name="name" length="50" />        </property>        <property name="birth" type="date">            <column name="birth" length="23" />        </property><!--    以下为手动添加部分--><!--    该标签用来定义继承student的子类与数据表的关系 --><joined-subclass name="org.vo.Bks"><!-- 子类的主键 --><key column="bksId"></key><!-- 子类中特有的属性 --><property name="ky" column="ky" type="boolean"></property></joined-subclass><joined-subclass name="org.vo.Yjs"><key column="yjsId"></key><property name="researchResult" column="researchResult" type="string"></property></joined-subclass> </class></hibernate-mapping>



注意:

1,student表id自动增长,bks和yjs的sid不自动增长,三张表建表时不需设置依赖关系

2,bks类和yjs类要继承student类

3,<generator class="native" /> 记得修改为native

4,bks和yjs的  *.hbm.xml  都删除,只在student.hbm.xml配置

5,在hibernate.cfg.xml中删除bks和yjs的映射





二:关联关系映射


1,一对一关联关系

2, 多对一关联关系

3,多对多关联关系




1 0
原创粉丝点击