hibernate 一对一(级联关系)

hibernate 核心配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
        
<hibernate-configuration>
    
    <session-factory>
        <!-- DataSource配置 -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        
        <!-- 指定连接数据库所使用的SQL方言-->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- 控制台是否输出SQL语句 -->
        <property name="show_sql">true</property>
        <!-- 式化Hibernate的SQL输出语句 -->
        <property name="hibernate.format_sql">true</property>
        <!-- 指定程序是否在数据库自动创建表 -->
        <property name="hbm2ddl.auto">update</property>
        <!-- 指定映射文件的路径 -->
        <mapping resource="zr/com/pojo/Company.hbm.xml"/>
        <mapping resource="zr/com/pojo/Employee.hbm.xml"/>
    </session-factory>

</hibernate-configuration>



        
        
        

实体类与数据库表映射关系配置文件

<?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="zr.com.pojo.Company" table="company">
        <!-- 主键生成策略 -->
        <id name="companyId" column="company_id">
            <generator class="native"/>
        </id>
        <property name="name" column="company_name"/>
        <!-- 一对一 -->
        <one-to-one name="employee" class="zr.com.pojo.Employee" property-ref="company" cascade="all" constrained="true"></one-to-one>
    </class>

</hibernate-mapping>
<?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="zr.com.pojo.Employee" table="employee">
        <!-- 主键生成策略 -->
        <id name="id" column="employee_id">
            <generator class="native"/>
        </id>
        <property name="name" column="employee_name"/>
        <!-- 一对一 -->
        <many-to-one name="company" class="zr.com.pojo.Company" column="company_id" cascade="all" unique="true">
        </many-to-one>
    </class>
</hibernate-mapping>
package zr.com.pojo;

public class Company {
    
    private int companyId;
    private String name;
    
    private Employee employee;
    
    
    public Employee getEmployee() {
        return employee;
    }
    public void setEmployee(Employee employee) {
        this.employee = employee;
    }
    public int getCompanyId() {
        return companyId;
    }
    public void setCompanyId(int companyId) {
        this.companyId = companyId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Company() {
        super();
    }
    public Company(int companyId, String name) {
        super();
        this.companyId = companyId;
        this.name = name;
    }
    @Override
    public String toString() {
        return "Company [companyId=" + companyId + ", name=" + name + "]";
    }
    
    
}
package zr.com.pojo;

public class Employee {
    
    private int id;
    
    private String name;
    
    private Company company;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public Employee() {
        super();
    }

    public Employee(int id, String name, Company company) {
        super();
        this.id = id;
        this.name = name;
        this.company = company;
    }

    @Override
    public String toString() {
        return "Employee [id=" + id + ", name=" + name + ", company=" + company
                + "]";
    }
    
    
    
    
}
package zr.com.test;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import zr.com.pojo.Company;
import zr.com.pojo.Employee;

public class TestClass {
    
    public static void main(String[] args) {
        
        // 创建Configuration 对象
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
        // 创建ServiceRegistry 对象
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
        // 创建SessionFactory 对象
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        // 创建Session 对象
        Session session = sessionFactory.openSession();
        
        // 开启事务
        session.beginTransaction();
        // 创建实体类
        
        Company company = new Company();
        company.setName("baidu");
        Employee employee = new Employee();
        employee.setName("lf");
        // 关联
        company.setEmployee(employee);
        employee.setCompany(company);
        // 插入数据
        session.save(employee);
        
        // 提交事务
        session.getTransaction().commit();
        
    
    }
}
原文地址:https://www.cnblogs.com/lantu1989/p/6626589.html