Hibernate N-1关联映射

117 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第24天,点击查看活动详情

前言

  Hibernate是一个开放源代码对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。

  对于单向的N—1关联而言只需要从N的一端可以访问1的一端。为了让这个两个持久化类支持这种关联映射,程序应该在N的一端的持久化类中增加一个熟悉,该属性引用1一端的关联实体。

1、实现思路:

 

  1. 创建数据库、数据表,person表和room表,person表的room_id字段为外键,且房间与人的关系是多对一的关系。

  2. 编写POJO实体类,属性与数据库一一对应,并生成Get、Set方法,且在Person注入实体类Room,因为对一关系中要让多的一方记住1的一方。

  3. 编写相应的hibernate配置文件,主键的生成策略等。

  4. 在为N的一方编写核心代码,多对一“many-to-one”

  5. 编写测试文件,且调用Session对象的save方法将person对象插入,这样才能做到都插入到数据库。

 

2、具体实现:

  编写实体类POJO,与数据库表一一对应。

package com.sqgxy.entity;

import java.io.Serializable;

public class Room implements Serializable{
    private Integer id;
    private String address;

    /*无参构造*/
    public Room() {
    }

    /*有参构造*/
    public Room(String address) {
        this.address = address;
    }

    /**
     * Get、Set
     * @return
     */
    。。。。。。。。。。

}
package com.sqgxy.entity;


import java.io.Serializable;

public class Person implements Serializable{
    private Integer id;
    private String name;
    private Room room;

    public Room getRoom() {
        return room;
    }

    public Person(){

    }

    public Person(String name){
        this.name = name;
    }

    public Person(String name, Room room) {
        this.name = name;
        this.room = room;
    }

    /**
     * Get、Set
     * @return
     * @param room
     */
    。。。。。。。。

}

Hibernate 配置文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!--使用 Hibernate 自带的连接池配置-->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">xmpkj</property>

        <!--hibernate 方言-->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>

        <!--打印sql语句-->
        <property name="hibernate.show_sql">true</property>
        <!--格式化sql-->
        <property name="hibernate.format_sql">true</property>

        <!-- 加载映射文件 -->
        <mapping resource="com/sqgxy/entity/Room.hbm.xml"/>
        <mapping resource="com/sqgxy/entity/Person.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

1、Room.hbm.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.sqgxy.entity.Room" table="room" schema="bianchengbang_jdbc">
        <!-- 主键-->
        <id name="id" column="id">
            <!--主键生成策略-->
            <generator class="native"></generator>
        </id>
        <property name="id" column="id" insert="false" update="false" />
        <property name="address" column="address"/>
    </class>
</hibernate-mapping>

2、Person.hbm.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.sqgxy.entity.Room" table="room" schema="bianchengbang_jdbc">
        <id name="id" column="id">
            <generator class="native"></generator>
        </id>
        <property name="id" column="id" insert="false" update="false" />
        <property name="address" column="address"/>
    </class>
</hibernate-mapping>

3、测试文件

Tes.java一定要调用Session对象的save方法将person对象插入,这样才能做到都插入到数据库。

package com.sqgxy.Test;


import com.sqgxy.entity.Person;
import com.sqgxy.entity.Room;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;


public class MyTest {

    /**
     * 查询数据库数据
     */
    @Test
    public void testQuery() {
        //Hibernate 加载核心配置文件(有数据库连接信息)
        Configuration configuration = new Configuration().configure();
        //创建一个 SessionFactory 用来获取 Session 连接对象
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        //获取session 连接对象
        Session session = sessionFactory.openSession();
        //开始事务
        Transaction transaction = session.beginTransaction();

        Room room = new Room();
        room.setAddress("D5");
        Person person = new Person();
        person.setName("赵云");
        person.setRoom(room);
        session.save(person);

        //提交事务
        transaction.commit();
        //释放资源
        session.close();
        sessionFactory.close();
    }
}

3、测试结果

1.原始数据

Person表数据

  image.png

 

Room数据

 image.png

2.要插入的数据

  image.png

 

3.插入成功

image.png

image.png

总结

  以上就是Hibernate N-1关联映射的全部内容,希望对你有所帮助。