Hibernate multi-to-Multi-two-way Association configuration, hibernate multi-to-one Association

Source: Internet
Author: User

Hibernate multi-to-Multi-two-way Association configuration, hibernate multi-to-one Association

There are two configuration methods for Hibernate's Bidirectional Multi-to-Multi-Association: Let's take a look at how the two solutions are configured.

1. Create a set of classes for Association

1. First, we need to add a set of two entity classes (employee <Emploee> and Project <Project>) to each other.

1.1 employee entity

Package cn. manytow.one; import java. util. HashSet; import java. util. Set; public class Emploee {// employee id private Integer empId;
// Project private String empName;
// Set private Set of the Project <Project> projects = new HashSet <Project> (); public Set <Project> getProjects () {return projects ;} public void setProjects (Set <Project> projects) {this. projects = projects;} public Integer getEmpId () {return empId;} public void setEmpId (Integer empId) {this. empId = empId;} public String getEmpName () {return empName;} public void setEmpName (String empName) {this. empName = empName ;}}

 

1.2 engineering entity

package cn.manytomany.one;import java.util.HashSet;import java.util.Set;public class Project {    private Integer proId;    private String proName;    private Set<Emploee> emploees=new HashSet<Emploee>();        public Set<Emploee> getEmploees() {        return emploees;    }    public void setEmploees(Set<Emploee> emploees) {        this.emploees = emploees;    }    public Integer getProId() {        return proId;    }    public void setProId(Integer proId) {        this.proId = proId;    }    public String getProName() {        return proName;    }    public void setProName(String proName) {        this.proName = proName;    }    }

 

 

2. With the entity class, we can configure the ing relationship between the object attributes and the table fields in the database.

2.1 emploees. hbm. xml

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 

 

2.2 projects. hbm. xml

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 

2.3 another important big configuration is to reference two small configurations.

<? Xml version = '1. 0' encoding = 'utf-8'?> <! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> </Session-factory> 

 

 

 

3. Finally, it is the test class.

Package cn. manytoyun. one; import org. hibernate. session; import org. hibernate. transaction; public class ManyToManyDoubleTest {/*** multiple-to-multiple bidirectional association test */public static void main (String [] args) {Session session = HibernateUtil. currentSession (); Transaction tsc = session. beginTransaction (); // create employee Emploee emp = new Emploee (); emp. setEmpName ("Tian Chao"); Emploee emp1 = new Emploee (); emp1.setEmpName ("Shi Qiang"); // create a Project pro = new Project (); pro. setProName ("Development Project"); pro. getEmploees (). add (emp); pro. getEmploees (). add (emp1); try {session. save (pro); tsc. commit ();} catch (Exception e) {// roll back tsc. rollback ();} HibernateUtil. closeSession ();}}

 

 

3.1 Add the tool class at the end. Just look at it.

Package cn. manytoyun. one; import org. hibernate. sessionFactory; import org. hibernate. cfg. configuration; import org. hibernate. session;/** session tool class */public class HibernateUtil {private static final ThreadLocal <Session> sessionTL = new ThreadLocal <Session> (); private static Configuration cfg; private static final SessionFactory sf; static {try {cfg = new Configuration (). configure (); sf = cfg. buildSessionFactory ();} catch (Exception e) {// Exception e. printStackTrace (); throw new ExceptionInInitializerError (e) ;}} public static Session currentSession () {Session session = sessionTL. get (); // if the session is null, open a new session if (session = null) {session = sf. openSession (); sessionTL. set (session) ;}return session;} public static void closeSession () {Session session = sessionTL. get (); sessionTL. set (null); session. close ();}}

 

 

2. Create an intermediate entity class for Association

1. Similar to the first solution, implement three entity classes first. The Code is as follows:

Package cn. manytother. doubleanother; import java. util. HashSet; import java. util. Set; public classEmploee{Private Integer empId; private String empName;Private Set <ProEmp> proemp = new HashSet <ProEmp> (); // The type of the Set is the object class type in the middle.Public Set <ProEmp> getProemp () {return proemp;} public void setProemp (Set <ProEmp> proemp) {this. proemp = proemp;} public Integer getEmpId () {return empId;} public void setEmpId (Integer empId) {this. empId = empId;} public String getEmpName () {return empName;} public void setEmpName (String empName) {this. empName = empName ;}}

 

 

package cn.manytomany.doubleanother;import java.util.HashSet;import java.util.Set;public class Project {    private Integer proId;    private String proName;
// The set type is still the intermediate object class type
Private Set <ProEmp> proemp = new HashSet <ProEmp> ();

Public Set <ProEmp> getProemp (){
Return proemp;
}
Public void setProemp (Set <ProEmp> proemp ){
This. proemp = proemp;
}
Public Integer getProId (){
Return proId;
}
Public void setProId (Integer proId ){
This. proId = proId;
}
Public String getProName (){
Return proName;
}
Public void setProName (String proName ){
This. proName = proName;
}

}


 

 

1.1 supplementary intermediate entity class

package cn.manytomany.doubleanother;public class ProEmp {        private Integer id;    private Emploee emp;    private Project pro;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public Emploee getEmp() {        return emp;    }    public void setEmp(Emploee emp) {        this.emp = emp;    }    public Project getPro() {        return pro;    }    public void setPro(Project pro) {        this.pro = pro;    }    }

 

 

2. The next step is the small configuration. The format is almost the same as that of the first solution. I just want to explain more. Just look at the small configuration.

Because we need to associate with the intermediate entity class, there is no eye to add between the employee class (Emploee) and the Project, just follow the normal configuration.

2.1 emploees. hbm. xml

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 

2.2 emploees. hbm. xml

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 

2.3 The key lies in proemp. hbm. xml (Converts multiple-to-multiple associations into two multiple-to-one associations.)

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><many-to-one name="emp" class="Emploee" column="EMPID">        </many-to-one>        <many-to-one name="pro" class="Project" column="PROID">        </many-to-one>    </class>

 

 

3. Now we can test the data.

Package cn. manytoyun. doubleanother; import org. hibernate. session; import org. hibernate. transaction; import cn. manytoyun. one. hibernateUtil; public class ManyToManyDoubleOnlyAnother {/*** multiple-to-many bidirectional Association --- two multiple-to-one Association */public static void main (String [] args) {Session session = HibernateUtil. currentSession (); Transaction tsc = session. beginTransaction (); // create employee Emploee emp = new Emploee (); emp. setEmpName ("Tian Chao"); // create a Project pro = new Project (); pro. setProName ("Development Project"); // intermediate class ProEmp proemp = new ProEmp (); proemp. setEmp (emp); proemp. setPro (pro); try {// Save the session. save (emp); session. save (pro); session. save (proemp); tsc. commit ();} catch (Exception e) {// roll back tsc. rollback ();} HibernateUtil. closeSession ();}}

 

 

All right, the two methods of multi-to-Multi-two-way Association of Hibernate have been completed. If you find it useful to you, please pay attention to it !!!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.