hibernate map Simply Implements One-to-Many Relational Mapping

Keywords: Hibernate Java Session xml

First of all, there are two tables of students (1) scores (more), which require hibernate relational mapping (xml) to establish one-to-many relationship between them. And use map to update students'grades
First, establish the entity class
Student.java

package entities;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/*
Variables are: school number, name, gender, date of birth, score (no field in the students table for pure mapping)
*/
public class Student {
    private Long studentNum;
    private String name;
    private String sex;
    private Date date;

    private Map<String,String> mark;

    public Student() {
        mark = new HashMap<>();
    }

    public Long getStudentNum() {
        return studentNum;
    }

    public void setStudentNum(Long studentNum) {
        this.studentNum = studentNum;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

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

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }


    public Map<String, String> getMark() {
        return mark;
    }

    public void setMark(Map<String, String> mark) {
        this.mark = mark;
    }
}

Then the relationship mapping file of Student is established.
Student.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="entities">
    <!-- Identifying a class xml mapping,name Class name,table Table name -->
    <class name="Student" table="students">
        <id name="studentNum" type="java.lang.Long">
            <column name="studentNum"/>
            <generator class="native"/>
        </id>
        <property name="name" type="java.lang.String" column="name"/>
        <property name="sex" type="java.lang.String" column="sex"/>
        <property name="date" type="java.util.Date" column="date"/>
        <property name="classNum" type="java.lang.Integer" column="class" />
        <map name="mark" table="marks">
            <key column="studentNum"/>
            <index type="java.lang.String" column="m_id" />
            <element type="java.lang.String" column="mark" />
        </map>
    </class>
</hibernate-mapping>

Explain here that in the class tag, name is the attribute name in Student, and column is the field name type in the students table to indicate the type of field value.
The hard part to understand is the map tag here.
The map tag is here to establish the relationship between two tables.
Name specifies the name of the map variable in Student table specifies the name of the table that establishes the relationship
The < key > label column attribute specifies the foreign key stuentNum of the marks table
<index/> or <map-key> are all for mapping key values in maps into marks tables
<element/> is for mapping map value values

Then comes the performance entity class.
It is noteworthy that a pair of more than one party is included as an entity class in the entity class.
Here is a student class in the score.
Mark.java

package entities;

public class Mark {
    private  Long studentNum;
    private String m_id;
    private String mark;
    private Student student;

    public Long getStudentNum() {
        return studentNum;
    }

    public void setStudentNum(Long studentNum) {
        this.studentNum = studentNum;
    }

    public String getM_id() {
        return m_id;
    }

    public void setM_id(String m_id) {
        this.m_id = m_id;
    }

    public String getMark() {
        return mark;
    }

    public void setMark(String mark) {
        this.mark = mark;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }
}

Finally, the corresponding mapping file of fractions
Mark.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="entities">
    <!-- Identifying a class xml mapping,name Class name,table Table name -->
    <class name="Mark" table="marks">

        <id name="studentNum" type="java.lang.Long">
            <column name="studentNum"/>
        </id>

        <!--Create tables to specify field types-->
        <property name="m_id" type="java.lang.String" column="m_id"/>
        <property name="mark" type="java.lang.String" column="mark"/>
    </class>
</hibernate-mapping>

Finally, put in the test code.

import entities.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Test {
    public static void main(String args[]){
        Configuration cfg = new Configuration();
        SessionFactory sf = cfg.configure().buildSessionFactory();
        Student s = new Student();
        s.setStudentNum(new Long(12312));
        s.getMark().put("aa","a22");
        s.getMark().put("123","11");
        Session session = sf.getCurrentSession();
        session.beginTransaction();
        session.save(s);
        session.getTransaction().commit();
    }
}

Posted by nezona on Sun, 21 Apr 2019 17:27:34 -0700