Hibernate relational mapping (three) and many-to-many relationship between Hibernate mapping (three) and many-to-many

First, many to one

Students and Student classes implement many-to-Grade, corresponding to a plurality of student class.

Student.java entity class, class attributes are mapped.

Copy the code
package com.lxit.entity;

import java.io.Serializable;

public class Student implements Serializable {
    
    public Student() {
    }

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

    public Student(String sname, String sex, String address, Grade grade) {
        this.sname = sname;
        this.sex = sex;
        this.address = address;
        this.grade = grade;
    }

    private int sid;
    private String sname;
    private String sex;
    address String Private;
    
    // unidirectional many-: entity objects in one of a plurality of packages of one of
    Private Grade Grade;
    
    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 String getSex () {
        return Sex;
    }
    public void setSex (String Sex) {
        this.sex Sex = ;
    }
    public String getAddress () {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

    public Grade getGrade() {
        return grade;
    }

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

    @Override
    public String toString() {
        return "Student [sid=" + sid + ", sname=" + sname + ", sex=" + sex
                + ", address=" + address + "]";
    }
Copy the code

 

Student.hbm.xml students mapping file, and add a class of many-relationship 

Copy the code
<?xml version="1.0"?>
<!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.lxit.entity.Student" table="Student">
         <id name="sid" column="sid">
             <generator class="native"></generator>
         </id>
         <property name="sname" column="sname" type="java.lang.String"></property>
         <property name="sex" column="sex"></ Property>          <-! realize one mapping relationship ->
         <Property name = "address" column = "address"> </ Property>

         <many-to-one name="grade" class="com.lxit.entity.Grade" column="gid" lazy="false"></many-to-one>
     </class>
Copy the code

</hibernate-mapping> 

 

 

Second, many 

Grade.java entity class mapped class and student-to-many, contains a collection of students. 

Copy the code
package com.lxit.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class Grade implements Serializable{
    
    public Grade() {
    }
    
    public Grade(String gname) {
        this.gname = gname;
    }
    
    //在一方添加多方的集合
    public Set<Student> students = new HashSet<Student>();


    private int gid;
    private String gname;
    
    public int getGid() {
        return gid;
    }
    public void setGid(int gid) {
        this.gid = gid;
    }
    public String getGname() {
        return gname;
    }
    public void setGname(String gname) {
        this.gname = gname;
    }

    public Set<Student> getStudents() {
        return students;
    }

    public void setStudents(Set<Student> students) {
        this.students = students;
    } 

Copy the code

 

Grade mapping file 

Copy the code
<?xml version="1.0"?>
<!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.lxit.entity.Grade" table="Grade">
         <id name="gid" column="gid">
             <generator class="native"></generator>
         </id>
         <property name="gname" type="java.lang.String">
             <column name="gname" length="20"></column>
         </ Property>              Cascade: Indicates cascading effect, when you save the class, the students saved with the corresponding class              inverse: friends said it would maintain the responsibility of parties to maintain the foreign key, default is false
         <-! 


         -->
         <set name="students" table="Student" cascade="all">
             <key column="gid"></key>
             <one-to-many class="com.lxit.entity.Student"/>
         </set>
     </class>

</hibernate-mapping> 

Copy the code

First, many to one

Students and Student classes implement many-to-Grade, corresponding to a plurality of student class.

Student.java entity class, class attributes are mapped.

Copy the code
package com.lxit.entity;

import java.io.Serializable;

public class Student implements Serializable {
    
    public Student() {
    }

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

    public Student(String sname, String sex, String address, Grade grade) {
        this.sname = sname;
        this.sex = sex;
        this.address = address;
        this.grade = grade;
    }

    private int sid;
    private String sname;
    private String sex;
    private String address;
    
    @ Unidirectional many-: entity objects in one of a plurality of packages of one of
    Private Grade Grade;
    
    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 String getSex () {
        return Sex;
    }
    public void setSex (String Sex) {
        this.sex Sex =;
    }
    public String getAddress () {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

    public Grade getGrade() {
        return grade;
    }

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

    @Override
    public String toString() {
        return "Student [sid=" + sid + ", sname=" + sname + ", sex=" + sex
                + ", address=" + address + "]";
    }
Copy the code

 

Student.hbm.xml students mapping file, and add a class of many-relationship 

Copy the code
<?xml version="1.0"?>
<!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.lxit.entity.Student" table="Student">
         <id name="sid" column="sid">
             <generator class="native"></generator>
         </id>
         <property name="sname" column="sname" type="java.lang.String"></property>
         <property name="sex" column="sex"></property>          <-! realize one mapping relationship ->
         <Property name = "address" column = "address"> </ Property>

         <many-to-one name="grade" class="com.lxit.entity.Grade" column="gid" lazy="false"></many-to-one>
     </class>
Copy the code

</hibernate-mapping> 

 

 

Second, many 

Grade.java entity class mapped class and student-to-many, contains a collection of students. 

Copy the code
package com.lxit.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class Grade implements Serializable{
    
    public Grade() {
    }
    
    public Grade(String gname) {
        this.gname = gname;
    }
    
    //在一方添加多方的集合
    public Set<Student> students = new HashSet<Student>();


    private int gid;
    private String gname;
    
    public int getGid() {
        return gid;
    }
    public void setGid(int gid) {
        this.gid = gid;
    }
    public String getGname() {
        return gname;
    }
    public void setGname(String gname) {
        this.gname = gname;
    }

    public Set<Student> getStudents() {
        return students;
    }

    public void setStudents(Set<Student> students) {
        this.students = students;
    } 

Copy the code

 

Grade mapping file 

Copy the code
<?xml version="1.0"?>
<!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.lxit.entity.Grade" table="Grade">
         <id name="gid" column="gid">
             <generator class="native"></generator>
         </id>
         <property name="gname" type="java.lang.String">
             <column name="gname" length="20"></column>
         </property>          ->              Cascade: Indicates cascading effect, when you save the class, the class corresponding with the students to save              Inverse: friends said it would maintain the responsibility of parties to maintain the foreign key, default is false
         <-! 



         <set name="students" table="Student" cascade="all">
             <key column="gid"></key>
             <one-to-many class="com.lxit.entity.Student"/>
         </set>
     </class>

</hibernate-mapping> 

Copy the code

Guess you like

Origin www.cnblogs.com/Jeely/p/11226179.html