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.xml 文件

<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 操作

3.生成的实体

public class Category {
    private Integer categoryId;
    private Set<Item> items = new HashSet<>();
}    

public class Item {
    private Integer itemId;
}

4.hbm 文件

<hibernate-mapping>
    <class name="com.nucsoft.hibernate.Item" table="item" schema="hibernate">
        <id name="itemId" column="item_id">
            <generator class="native"/>
        </id>
    </class>
</hibernate-mapping>
<hibernate-mapping package="com.nucsoft.hibernate">
    <class name="Category" table="category" schema="hibernate">
        <id name="categoryId" column="category_id">
            <generator class="native"/>
        </id>
        <!-- table 指中间表 -->
        <set name="items" table="categories_items" schema="hibernate">
            <key>
                <!-- category 在中间表中的列名 -->
                <column name="category_id"/>
            </key>
            <!-- class Set 集合中持久化类的类名, column Set集合中的持久化类咋中间表的外键列的名称 -->
            <many-to-many not-found="ignore" class="Item">
                <column name="item_id"/>
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>

六、双向 n 对 n

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

2.Intellij Idea 操作

3.生成的实体

public class Category {
    private Integer categoryId;
    private Set<Item> items = new HashSet<>();
}    

public class Item {
    private Integer itemId;
    private Set<Category> categories = new HashSet<>();
}

4.hbm文件

<hibernate-mapping package="com.nucsoft.hibernate" schema="hibernate">
    <class name="Category" table="category">
        <id name="categoryId" column="category_id">
            <generator class="native"/>
        </id>
        <set name="items" table="categories_items">
            <key>
                <column name="category_id"/>
            </key>
            <!-- 交叉对应 -->
            <many-to-many not-found="ignore" class="Item">
                <column name="item_id"/>
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>

<hibernate-mapping package="com.nucsoft.hibernate" schema="hibernate">
    <class name="Item" table="item">
        <id name="itemId" column="item_id">
            <generator class="native"/>
        </id>
        <set name="categories" inverse="true" table="categories_items">
            <key>
                <column name="item_id"/>
            </key>
            <!-- 交叉对应 -->
            <many-to-many not-found="ignore" class="Category">
                <column name="category_id"/>
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>
posted @ 2016-10-12 16:56  solverpeng  阅读(376)  评论(0编辑  收藏  举报