你的位置:首页 > Java教程

[Java教程]Hibernate —— 映射关联关系(附录)


一、单向的多对一

1.建表语句

CREATE TABLE customer ( customer_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY ,)CREATE TABLE `order` ( order_id  INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, customer_id INT(11))

2.Intellij Idea 操作

3.Order.hbm.

<hibernate-mapping>  <class name="com.nucsoft.hibernate.Order" table="order" schema="hibernate">    <id name="orderId">      <column name="order_id" sql-type="int(11)"/>      <generator class="native"/>    </id>    <many-to-one name="customer" class="com.nucsoft.hibernate.Customer">      <!-- 外键名称 -->      <column name="customer_id" not-null="true"/>    </many-to-one>  </class></hibernate-mapping>

二、双向的多对一

1.建表语句和单向的多对一相同

2.Intellij Idea 操作

3.生成的实体

public class Customer {  private Integer customerId;  private Set<Order> orders = new HashSet<>();//注意进行初始化}public class Order {  private Integer orderId;  private Customer customer;}

4.hbm 文件

<hibernate-mapping>  <class name="com.nucsoft.hibernate.Customer" table="customer" schema="hibernate">    <id name="customerId">      <column name="customer_id" sql-type="int(11)"/>      <generator class="native"/>    </id>    <set name="orders" inverse="true">      <key>        <column name="customer_id" not-null="true"/>      </key>      <one-to-many not-found="ignore" class="com.nucsoft.hibernate.Order"/>    </set>  </class></hibernate-mapping><hibernate-mapping>  <class name="com.nucsoft.hibernate.Order" table="order" schema="hibernate">    <id name="orderId">      <column name="order_id" sql-type="int(11)"/>      <generator class="native"/>    </id>    <many-to-one name="customer" class="com.nucsoft.hibernate.Customer">      <column name="customer_id" not-null="true"/>    </many-to-one>  </class></hibernate-mapping>

三、基于外键的双向一对一

1.建表语句

CREATE TABLE department(  dept_id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,  manager_id_fk INT(11));CREATE TABLE manager(  manager_id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,);

2.生成的实体

public class Department {  private Integer deptId;  private Manager manager;}public class Manager {  private Integer managerId;  private Department dept;}

3.hbm文件

<hibernate-mapping>  <class name="com.nucsoft.hibernate.Department" table="department" schema="hibernate">    <id name="deptId" column="dept_id">      <generator class="native"/>    </id>    <many-to-one name="manager" class="com.nucsoft.hibernate.Manager" column="manager_id_fk" unique="true"/>  </class></hibernate-mapping><hibernate-mapping>  <class name="com.nucsoft.hibernate.Manager" table="manager" schema="hibernate">    <id name="managerId" column="manager_id">      <generator class="native"/>    </id>    <one-to-one name="dept" class="com.nucsoft.hibernate.Department" property-ref="manager"/>  </class></hibernate-mapping>

四、基于主键的双向一对一

1.建表语句

CREATE TABLE department ( dept_id  INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,);CREATE TABLE manager ( manager_id  INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,);

2.生成的实体

public class Department {  private Integer deptId;  private Manager manager;}public class Manager {  private Integer managerId;  private Department dept;}

3.hbm文件

<hibernate-mapping><class name="com.nucsoft.hibernate.Department" table="department" schema="hibernate">  <id name="deptId" column="dept_id">    <generator class="foreign">      <param name="property">manager</param>    </generator>  </id>  <one-to-one name="manager" class="com.nucsoft.hibernate.Manager" constrained="true"/></class></hibernate-mapping><hibernate-mapping><class name="com.nucsoft.hibernate.Manager" table="manager" schema="hibernate">  <id name="managerId" column="manager_id">    <generator class="native"/>  </id>  <one-to-one name="dept" class="com.nucsoft.hibernate.Department"/></class></hibernate-mapping>

五、单向n对n

1.建表语句

CREATE TABLE category ( category_id  INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,);CREATE TABLE item ( item_id  INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,);CREATE TABLE categories_items ( category_id INT(11) NOT NULL, item_id   INT(11) NOT NULL);

2.Intellij Idea 操作


六、双向 n 对 n

1.建表语句和单向 n 对 n 相同

2.Intellij Idea 操作