Multiple-to-many configuration of Hibernate Association ing and multiple-to-many configuration of hibernate

Source: Internet
Author: User

Multiple-to-many configuration of Hibernate Association ing and multiple-to-many configuration of hibernate

We will talk about the many-to-many configuration of association ing, which is not easy to configure. So, we will put it here for your reference.

First:CreateMany-to-manySplit two-way association into one-to-multiple

Three entity classes

Employee

Private Integer empno; private String ename; private Dept dept; private Set <Project> projects = new HashSet <Project> (0); // attributes of the Set class employeePrivate Set <ProEmp> pe = new HashSet <ProEmp> (0 );

Project

Private Integer proNo; // Project ID private String proName; // project name private Set <Employee> employees = new HashSet <Employee> (0); // Set attributes of the class 'employe'Private Set <ProEmp> pe = new HashSet <ProEmp> (0 );

Intermediate table

ProEmp

    private Integer id;    private Project pro;    private Employee emp;

Ing File

Employee. 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">    

Project. 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">    

ProEmp. hbm. xml

<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <! -- Associate with employees --> <strong-to-one name = "emp" column = "empno" class = "Employee"> </strong-to-one> <! -- Associate with a project --> <role-to-one name = "pro" class = "Project" column = "proNo"> </role-to-one></Class> 

Large configuration file hibernate. cfg. xml

<? 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">       <Mapping resource = "cn/three/entity/Dept. hbm. xml "/> <mapping resource =" cn/three/entity/Employee. hbm. xml "/> <mapping resource =" cn/three/entity/Project. hbm. xml "/></Session-factory> 

Test class to see the effect

Package cn. main; import org. hibernate. session; import cn. three. entity. proEmp; import cn. three. entity. employee; import cn. three. entity. project; import cn. util. hibernateUtil; public class ThreeTest {public static void main (String [] args) {// Merge () together with three tables;} private static void threeAddEmpAndProAndProEmp () {Session session Session = HibernateUtil. currentSession (); session. beginTransaction (); Employee emp = new Employee (); Project pro = new Project (); emp. setEname ("Li Si"); pro. setProName ("Project 1"); ProEmp pe = new ProEmp ();Pe. setEmp (emp); pe. setPro (pro); session. save (pro); session. save (emp); session. save (pe );Session. getTransaction (). commit (); HibernateUtil. closeSessio (); System. out. println ("add OK ");}}

Type 2: Do not split it into two one-to-multiple

You only need two ing files.

Employee. hbm. xml

<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <Role-to-one name = "dept" column = "deptNo" class = "Dept"> </role-to-one> <! -- Multiple-to-multiple bidirectional Association --> <set name = "projects" inverse = "true" table = "proemp"> <key column = "rempNo"> </key> <strong -to-operate class = "Project" column = "rproNo"> </allow-to-operate> </set></Class> 

Project. hbm. xml

<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <! -- Configure one-way, many-to-many Association --> <! -- Map the attributes of employees in the project class --> <! -- Cascade = "save-update": when the table name is used to save or update the project object, the associated Employee object is saved or updated in cascade mode --> <set name = "employees" table = "proemp" cascade = "save-update"> <key column = "rproNo "> </key> <strong-to-class =" Employee "column =" rempNo "> </strong-to-short> </set></Class> 

Test class

Private static void addProToEmpAndEmpToPro () {Session session = HibernateUtil. currentSession (); session. beginTransaction (); Employee emp = new Employee (); Project pro = new Project (); emp. setEname ("Li Si"); pro. setProName ("Project 1 ");Pro. getEmployees (). add (emp); session. save (pro );Session. getTransaction (). commit (); HibernateUtil. closeSessio (); System. out. println ("add OK ");

In this way, you can answer questions online !!!!! 11111111

 

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.