Tag:Hibernate
Article From:https://www.cnblogs.com/DawnCHENXI/p/9124565.html

 

 

 

————I have no him, but my hands are ripe, humility is foolishness, but learning is hunger.

 

 

 

This blog will tellHibernateMulti to multi correlation in the caseThe operation of the relationship is less prepared.

 

1.Talk about many to many

  Many to many relationships, such as students and teachers, a student is taught by multiple teachers, and one teacher is taught to many students. This is many to many.

  Tell aboutMany to many, first of all, there must be a table called the middle table.

Two, the creation of the entity class

  1.StuStudent class creation:

 

package cn.dawn.day04.entity;

/**
 * Created by Dawn on 2018/6/2.
 */
import java.io.Serializable;
import java.util.Set;

/*Student entity class*/
public class Stu implements Serializable{
    private Long sid;
    private String sname;
    /*Set of teacher objects*/
    private Set<Tea> teas;
    public Long getSid() {
        return sid;
    }

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

    public String getSname() {
        return sname;
    }

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

    public Set<Tea> getTeas() {
        return teas;
    }

    public void setTeas(Set<Tea> teas) {
        this.teas = teas;
    }
}

 

  2.TeaThe creation of classrooms:

 

package cn.dawn.day04.entity;

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

/**
 * Created by Dawn on 2018/6/2.
 */
/*Teacher entity class*/
public class Tea implements Serializable {
    private Long tid;
    private String tname;
    /*Student object set*/
    private Set<Stu> stus;

    public Long getTid() {
        return tid;
    }

    public void setTid(Long tid) {
        this.tid = tid;
    }

    public String getTname() {
        return tname;
    }

    public void setTname(String tname) {
        this.tname = tname;
    }

    public Set<Stu> getStus() {
        return stus;
    }

    public void setStus(Set<Stu> stus) {
        this.stus = stus;
    }
}

 

Three, the creation of a hbm.xml file

  1.Stu.hbm.xml

 

<?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 package="cn.dawn.day04.entity">
    <!--If package is specified above, the class name does not need to write the whole class name.-->
    <!--lazy:Lazy loading (lazy loading) the default value is true, lazy loading.-->
    <!--<class name="Teacher">-->
    <!--Direct loading-->
    <class name="Stu" lazy="false">
        <!--Primary key-->
        <id name="sid" column="sid">
            <!--Key key generation of assigned programmers themselves-->
            <!--identityIt is the self increment in mysql, which will increase the operation and do not need to assign the primary key again.-->
            <!--incrementFirst, check the largest primary key column and add one to the primary key in the next column.-->
            <!--sequenceIt is the primary key of oracle that generates the strategy. He will need to specify the sequence name < param name= "sequence" > sequence name < /param>-->
            <generator class="increment"></generator>
        </id>
        <property name="sname" column="tname"></property>
        <!--tableRefers to an intermediate table, a table that carries an association relationship-->
        <set name="teas" table="tea_stu" cascade="save-update" inverse="false">
            <key>
                <!--The ID of this class table-->
                <column name="sid"></column>
            </key>
            <!--In addition to the multi - related entity class that he has-->
            <many-to-many class="Tea" column="tid"></many-to-many>
        </set>
    </class>
</hibernate-mapping>

 

  2.Tea.hbm.xmlThe creation of a document:

 

<?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 package="cn.dawn.day04.entity">
    <!--If package is specified above, the class name does not need to write the whole class name.-->
    <!--lazy:Lazy loading (lazy loading) the default value is true, lazy loading.-->
    <!--<class name="Teacher">-->
    <!--Direct loading-->
    <class name="Tea" lazy="false">
        <!--Primary key-->
        <id name="tid" column="tid">
            <!--Key key generation of assigned programmers themselves-->
            <!--identityIt is the self increment in mysql, which will increase the operation and do not need to assign the primary key again.-->
            <!--incrementFirst, check the largest primary key column and add one to the primary key in the next column.-->
            <!--sequenceIt is the primary key of oracle that generates the strategy. He will need to specify the sequence name < param name= "sequence" > sequence name < /param>-->
            <generator class="increment"></generator>
        </id>
        <property name="tname" column="tname"></property>
        <!--tableRefers to an intermediate table, a table that carries an association relationship-->
        <set name="stus" table="tea_stu" cascade="save-update" inverse="false">
            <key>
                <!--The ID of this class table-->
                <column name="tid"></column>
            </key>
            <!--In addition to the multi - related entity class that he has-->
            <many-to-many class="Stu" column="sid"></many-to-many>
        </set>
    </class>
</hibernate-mapping>

 

Four,hibernate.cfg.xmlTwo hbm.xml small configuration files above management

        <!--Mappings with small configuration files-->
        <mapping resource="cn/dawn/day04/dao/Tea.hbm.xml"></mapping>
        <mapping resource="cn/dawn/day04/dao/Stu.hbm.xml"></mapping>

Five, single test method:

 

    @Test
    /*Save it at the same time and establish a relationship*/
    public void t1ManyToManyInsert(){
        /*
         * A new teacher*/
        Tea tea1=new Tea();
        tea1.setTname("Old rain ");
        /**
         * Two new students*/
        Stu stu1=new Stu();
        stu1.setSname("Small six ");
        Stu stu2=new Stu();
        stu2.setSname("Small eight ");

        Set<Stu> students = new HashSet<Stu>();
        students.add(stu1);
        students.add(stu2);
        /**
         * Establishing the relationship between the students and the curriculum through the course*/
        tea1.setStus(students);//Because the curriculum is new, so there is no student.
        /*Newly added*/
        session.save(tea1);
        /*Transaction submission*/
        tr.commit();



        /*
        Hibernate: create table Stu (sid bigint not null, tname varchar(255), primary key (sid)) engine=MyISAM
        Hibernate: create table Tea (tid bigint not null, tname varchar(255), primary key (tid)) engine=MyISAM
        Hibernate: create table tea_stu (tid bigint not null, sid bigint not null, primary key (sid, tid)) engine=MyISAM
        Hibernate: alter table tea_stu add constraint FK1lo22k662lgxr7d79rmfajlu3 foreign key (sid) references Stu (sid)
        Hibernate: alter table tea_stu add constraint FKgq01y04i2r2ye5pd8abh5x42u foreign key (tid) references Tea (tid)
        Hibernate: select max(tid) from Tea
        Hibernate: select max(sid) from Stu
        Hibernate: insert into Tea (tname, tid) values (?, ?)
        Hibernate: insert into Stu (tname, sid) values (?, ?)
        Hibernate: insert into Stu (tname, sid) values (?, ?)
        Hibernate: insert into tea_stu (tid, sid) values (?, ?)
        Hibernate: insert into tea_stu (tid, sid) values (?, ?)
        * */
    }

 

Six, summary:

  I think this is enough. The best way to learn is to try and try it yourself.

  I can raise the demand:

    1.Save a single student

    2.Amend a student associated with a teacher

    3.Get rid of all the teachers associated with a student

    And so on.

 

Leave a Reply

Your email address will not be published. Required fields are marked *