Hibernate关系映射之多对一

多对一:通过在多方持有一方的引用实现,需要在”多“的一端使用<many-to-one>配置

1、插入数据(学生、班级)
**Grade.java**
package com.lxj.entity;

public class Student {
    private int sid;
    private String sname;
    private int sex;
    // 多方定义一个一方的引用
    private Grade grade;

    public Grade getGrade() {
        return grade;
    }

    public void setGrade(Grade grade) {
        this.grade = grade;
    }

    public Student(String sname, int sex) {
        this.sname = sname;
        this.sex = sex;
    }

    public Student(){

    }



    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (sid != student.sid) return false;
        if (sex != student.sex) return false;
        if (sname != null ? !sname.equals(student.sname) : student.sname != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = sid;
        result = 31 * result + (sname != null ? sname.hashCode() : 0);
        result = 31 * result + sex;
        return result;
    }
}

**Grade.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.lxj.entity.Grade" table="grade" schema="hibernate_study">
        <id name="gid" column="gid" type="java.lang.Integer">
            <generator class="increment"></generator>
        </id>
        <property name="gname" column="gname" type="java.lang.String" />
        <property name="gdesc" column="gdesc" type="java.lang.String"/>
    </class>
</hibernate-mapping>


**Student.java**
package com.lxj.entity;

public class Student {
    private int sid;
    private String sname;
    private int sex;
    // 多方定义一个一方的引用
    private Grade grade;

    public Grade getGrade() {
        return grade;
    }

    public void setGrade(Grade grade) {
        this.grade = grade;
    }

    public Student(String sname, int sex) {
        this.sname = sname;
        this.sex = sex;
    }

    public Student(){

    }



    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (sid != student.sid) return false;
        if (sex != student.sex) return false;
        if (sname != null ? !sname.equals(student.sname) : student.sname != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = sid;
        result = 31 * result + (sname != null ? sname.hashCode() : 0);
        result = 31 * result + sex;
        return result;
    }
}

**Student.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.lxj.entity.Student" table="student" schema="hibernate_study">
        <id name="sid" column="sid" type="java.lang.Integer">
            <generator class="increment"></generator>
        </id>
        <property name="sname" column="sname"/>
        <property name="sex" column="sex"/>

        <!--配置多对一关联关系-->
        <!--
            name:当前实体关联的字段
            class:关联那个实体
            column:关联的实体对应的字段
        -->
        <many-to-one name="grade" class="com.lxj.entity.Grade" column="gid"></many-to-one>
    </class>
</hibernate-mapping>



**HibernateUtil.java**
package com.lxj.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static SessionFactory sessionFactory;
    private static Session session;
    private static Transaction transaction;

    static {
        Configuration configuration = new Configuration().configure();
        sessionFactory = configuration.buildSessionFactory();
    }

    /**
     * 获取session工厂
     * @return
     */
    public static SessionFactory getSessionFactory(){
        return sessionFactory;
    }

    /**
     * 获取session
     * @return
     */
    public static Session getSession(){
        session = sessionFactory.openSession();
        return session;
    }

    /**
     * 关闭session
     */
    public static void closeSession(){
        if(session != null){
            session.close();
        }
    }
}

**hibernate.cfg.xml**
<?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>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate_study</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>

        <!-- 配置hibernate的基本信息 -->

        <!-- 配置数据库方言dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <!-- 指定自动生成数据表的策略 -->
        <property name="hbm2ddl.auto">update</property>


        <mapping resource="com/lxj/entity/Grade.hbm.xml"/>
        <mapping resource="com/lxj/entity/Student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>



**TestManyToOne**

import com.lxj.entity.Grade;
import com.lxj.entity.Student;
import com.lxj.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

public class TestManyToOne {

    @Test
    public void save(){

        Grade g  = new Grade("java 1","1班");
        Student student1 = new Student("女神",1);
        Student student2 = new Student("男神",0);

        student1.setGrade(g);
        student2.setGrade(g);

        Session session = HibernateUtil.getSession();
        Transaction transaction = session.beginTransaction();
        session.save(g);
        session.save(student1);
        session.save(student2);
        transaction.commit();
        HibernateUtil.closeSession();

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值