hibernate基础04:关联关系(多对一)

Posted 来临

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate基础04:关联关系(多对一)相关的知识,希望对你有一定的参考价值。

 

一、对象信息 转换为 关系信息(数据表)

1、使用SchemaExport生成关系

package com.project.test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.project.pojo.User;
import com.project.util.HibernateUtil;

public class HibernateTest {
    Session session = null;
    Transaction beginTransaction = null;
    
    @Before
    public void setUp(){
        session = HibernateUtil.getSession();
        beginTransaction = session.beginTransaction();
    }
    
    @After
    public void tearDown(){
        HibernateUtil.closeSession();
    }
    
    
    @Test
    public void testSelect(){
        User u = (User) session.get(User.class, 1);
        System.out.println(u);
    }
    
    @Test
    public void testCreateDB(){
        Configuration cfg = new Configuration().configure();
        //使得hibernate映射信息转换为数据库识别的dll语言
        SchemaExport se = new SchemaExport(cfg);
        //第一个参数:是否打印ddl语句
        //第二个参数:是否将ddl到数据库中执行
        se.create(true, true);
    }
}

2、hibernate.cxf.xml 通用配置 hbm2ddl

<!-- 
            hbm2ddl.auto 该属性用于配置是否将映射信息转换为ddl
            create 每次执行都会先删除后再创建表,用于测试
            update 检查数据库表是否存在,如果不存在则先创建表,项目中通常上线使用
         -->
        <property name="hbm2ddl.auto">create</property>

二、关联关系:多对一

    多   对   1

  student(学生)——》classes(班级)

1、classes类

package com.project.pojo;

import java.io.Serializable;

public class Classes implements Serializable{
    private int id;
    private String name;
    private String address;
    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 String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    
    
}

2、学生类

package com.project.pojo;

import java.io.Serializable;

public class Student implements Serializable{
    private int id;
    private String name;
    private int age;
    private Classes classes;
    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 int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Classes getClasses() {
        return classes;
    }
    public void setClasses(Classes classes) {
        this.classes = classes;
    }
    
}

3、Classes.hbm2.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 package="com.project.pojo">
    <class name="Classes" table="t_class">
        <id name="id" column="id" type="int">
            <generator class="native"></generator>
        </id>
        <property name="name" />
        <property name="address" />
    </class>

</hibernate-mapping>

4、Student.hbm2.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">
<!-- package指定类所在的包,如果不指定,那么在指定class的name时,需要指定完全限定名 -->
<hibernate-mapping package="com.project.pojo">
    <class name="Student" table="t_student">
        <id name="id">
            <generator class="native"></generator>
        </id>
        <property name="name" />
        <property name="age" />
        <many-to-one name="classes" column="classes_id" />
    </class>

</hibernate-mapping>

5、hibernate.cfg.xml

<mapping resource="com/project/pojo/Classes.hbm2.xml" />
        <mapping resource="com/project/pojo/Student.hbm2.xml" />

6、测试

@Test
    public void testSave(){
        try {
            Classes c1 = new Classes();
            c1.setName("一年级");
            c1.setAddress("B-1栋");
            
            Classes c2 = new Classes();
            c2.setName("二年级");
            c2.setAddress("B-2栋");
            
            session.save(c1);
            session.save(c2);
            
            Student s1 = new Student();
            s1.setName("张三");
            s1.setAge(20);
            s1.setClasses(c1);
            
            Student s2 = new Student();
            s2.setName("李四");
            s2.setAge(21);
            s2.setClasses(c2);
            
            Student s3 = new Student();
            s3.setName("哈哈狗");
            s3.setAge(19);
            s3.setClasses(c1);
            
            session.save(s1);
            session.save(s2);
            session.save(s3);
            
            ts.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if(ts!=null) ts.rollback();
        }
    
    }

 

类与类关系:依赖——》关联——》聚合——》组合         根据箭头类与类关系强度越来越高

以上是关于hibernate基础04:关联关系(多对一)的主要内容,如果未能解决你的问题,请参考以下文章

hibernate 关联关系(多对多对多一对一)的配置

一口一口吃掉Hibernate——多对一单向关联映射

Hibernate关联关系配置(一对多一对一和多对多)

hibernate关联关系

Hibernate的关联关系映射

hibernate映射的 关联关系:有 一对多关联关系,一对一关联关系,多对多关联关系,继承关系