Hibernate学习笔记一

最近在学习Hibernate,东西琐碎,就想记记笔记,以备以后自己的查看,也希望能帮助大家。如有错误,请大家提出意见。


一对多双向关联关系:

Customer.java:

   private long id;

   private String name;

   private Set<Order> orders;

Order.java:

   private long id;

   private String orderNumber;

   private Customer customer;

为其生成setget方法。

Customer.hbm.xml:

<classname="com.songjinghao.hibernate.Customer"table="customers">

      <idname="id"column="id"type="long">

        <generatorclass="increment"></generator>

      </id>

      <propertyname="name"column="name"type="string"></property>

      <setname="orders"cascade="all"inverse="true"lazy="false">

        <keycolumn="customer_id"></key>

        <one-to-manyclass="com.songjinghao.hibernate.Order"/>

      </set> 

</class>

 

Order.hbm.xml:

<classname="com.songjinghao.hibernate.Order"table="orders">

      <idname="id"column="id"type="long">

        <generatorclass="increment"></generator>

      </id>

      <propertyname="orderNumber"column="order_Number"type="string"></property>

      <many-to-onename="customer"class="com.songjinghao.hibernate.Customer"column="customer_id"lazy="false">

      </many-to-one>

</class>

 

一对多双向自身关联关系:

Category.java:

   private long id;

   private String name;

   private Category parentCategory;

   private Set<Category> childCategories;

为其生成setget方法和两个构造方法。

Category.hbm.xml

<classname="com.songjinghao.hibernate.Category"table="categories">

      <idname="id"column="id"type="long">

        <generatorclass="increment"></generator>

      </id>

      <propertyname="name"column="name"type="string"></property>

      <setname="childCategories"cascade="all"inverse="true">

        <keycolumn="category_id"></key>

        <one-to-manyclass="com.songjinghao.hibernate.Category"/>

      </set> 

      <many-to-onename="parentCategory"column="category_id"class="com.songjinghao.hibernate.Category">

      </many-to-one>

</class>

 

 一对一关联关系:

1)主键关联

Student.java:

   private String id;

   private String name;

   private IdCard idCard;

为其生成setget方法。

IdCard.java:

   private String id;

   private int number;

   private Student student;

为其生成setget方法。

Student.hbm.xml:

<classname="com.songjinghao.hibernate.Student"table="student">

      <idname="id"column="id"type="string">

        <generatorclass="uuid"></generator>

      </id>  

      <propertyname="name"column="name"type="string"></property>

      <one-to-onename="idCard"class="com.songjinghao.hibernate.IdCard"cascade="all"fetch="select"></one-to-one>

</class>

 

IdCard.hbm.xml:

<classname="com.songjinghao.hibernate.IdCard"table="idCard">

      <idname="id"column="id"type="string">

        <generatorclass="foreign">

           <paramname="property">student</param>

        </generator>

      </id>

      <propertyname="number"column="number"type="integer"></property>

      <one-to-onename="student"class="com.songjinghao.hibernate.Student"fetch="select"></one-to-one>

</class>

总结:

一对一默认使用的是立即加载,如果需要使用延迟加载,那么需要在one-to-one元素中将constrained属性设为true,并且将待加载的一方的class元素中的lazy属性设为true(或者不去设置,因为该属性默认值就是true)。一对一加载时默认使用左外连接,可以通过修改fetch属性为select修改成每次发送一条select语句的形式。

 

2)外键关联

只需改动IdCard.hbm.xml文件:

<classname="com.songjinghao.hibernate.IdCard"table="idCard">

      <!--

      <id name="id" column="id"type="string">

        <generator class="foreign">

           <param name="property">student</param>

        </generator>

      </id>

       -->

      <idname="id"column="id"type="string">

        <generatorclass="uuid"></generator>

      </id>

      <propertyname="number"column="number"type="integer"></property>

      <!--

      <one-to-one name="student"class="com.songjinghao.hibernate.Student"fetch="select"></one-to-one>

       -->

      <many-to-onename="student"class="com.songjinghao.hibernate.Student"column="student_id"unique="true"></many-to-one>

</class>

总结:

本质上是一对多的蜕化形式。在many-to-one元素中增加unique=true”属性就变成了一对一。

 

一对多与一对一结合:


Student.java:

   private String id;

   private String name;

   private IdCard idCard;

   private Team team;

为其生成setget方法。

IdCard.java:

   private String id;

   private int number;

   private Student student;

为其生成setget方法。

Team.java:

   private String id;

   private String name;

   private Set<Student> students;

为其生成setget方法。

Student.hbm.xml:

<classname="com.songjinghao.hibernate.Student"table="student">

      <idname="id"column="id"type="string">

        <generatorclass="uuid"></generator>

      </id>

      <propertyname="name"column="name"type="string"></property>

      <one-to-onename="idCard"class="com.songjinghao.hibernate.IdCard"cascade="all"constrained="true"fetch="select"></one-to-one>

      <many-to-onename="team"class="com.songjinghao.hibernate.Team"column="team_id"cascade="all"fetch="select">

      </many-to-one>     

</class>

IdCard.hbm.xml:

<classname="com.songjinghao.hibernate.IdCard"table="idCard"lazy="false">

      <idname="id"column="id"type="string">

        <generatorclass="foreign">

           <paramname="property">student</param>

        </generator>

      </id>

      <propertyname="number"column="number"type="integer"></property>    

      <one-to-onename="student"class="com.songjinghao.hibernate.Student"cascade="none"fetch="select"></one-to-one>

</class>

Team.hbm.xml:

<classname="com.songjinghao.hibernate.Team"table="team">

      <idname="id"column="id"type="string">

        <generatorclass="uuid"></generator>

      </id>

      <propertyname="name"column="name"type="string"></property>

      <setname="students"cascade="all"lazy="false"inverse="true">

        <keycolumn="team_id"></key>

        <one-to-manyclass="com.songjinghao.hibernate.Student"/>

      </set>

</class>

 

多对多关联关系:

Hibernate学习笔记一_第1张图片

Student.java:

   private String id;

   private String name;

   private Set<Course> courses;

为其生成setget方法。

Course.java:

private String id;

   private String name;

   private Set<Student> students;

为其生成setget方法。

Student.hbm.xml:

<classname="com.songjinghao.hibernate.Student"table="student">

      <idname="id"column="id"type="string">

        <generatorclass="uuid"></generator>

      </id>

      <propertyname="name"column="name"type="string"></property>

      <setname="courses"table="student_course"cascade="save-update">

        <keycolumn="student_id"></key>

        <many-to-manyclass="com.songjinghao.hibernate.Course"column="course_id"></many-to-many>

      </set>

</class>

Course.hbm.xml:

<classname="com.songjinghao.hibernate.Course"table="course">

      <idname="id"column="id"type="string">

        <generatorclass="uuid"></generator>

      </id>

      <propertyname="name"column="name"type="string"></property>

      <setname="students"table="student_course"cascade="save-update"inverse="true">

        <keycolumn="course_id"></key>

        <many-to-manyclass="com.songjinghao.hibernate.Student"column="student_id"></many-to-many>

      < /set >

</class>


你可能感兴趣的:(Hibernate学习笔记一)