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

2021-02-19 16:21

阅读:459

标签:code   serial   bsp   ble   cep   项目   数据表   组合   system   

 

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

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


        property name="hbm2ddl.auto">createproperty>

二、关联关系:多对一

    多   对   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">

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:关联关系(多对一)

标签:code   serial   bsp   ble   cep   项目   数据表   组合   system   

原文地址:https://www.cnblogs.com/chai-blogs/p/12927279.html


评论


亲,登录后才可以留言!