Hibernate Many2many Mapping Relationship

Source: Internet
Author: User

Many-to-many mapping relationships:

Development steps:

JavaBean Mapping File Test

Detailed Picture:


1.javaBean

Project.java

Package Cn.itcast.many2many;import Java.util.hashset;import Java.util.set;public class Project {private int proj_id;@ Overridepublic String toString () {return "Project [proj_id=" + proj_id + ", proj_name=" + proj_name+ ", developers=" + Dev Elopers + "]";} Private String proj_name;private set<developer> developers=new hashset<developer> ();p ublic int getProj_id () {return proj_id;} public void setproj_id (int proj_id) {this.proj_id = proj_id;} Public String Getproj_name () {return proj_name;} public void Setproj_name (String proj_name) {this.proj_name = Proj_name;} Public set<developer> Getdevelopers () {return developers;} public void Setdevelopers (set<developer> developers) {this.developers = developers;}}
Employee.java

Package Cn.itcast.many2many;import Java.util.hashset;import Java.util.set;public class Developer {@Overridepublic String toString () {return "Developer [d_id=" + d_id + ", d_name=" + D_name + ", projects=" + Projects + "]";} private int d_id;private String d_name;private set<project> projects=new hashset<project> ();p ublic int GetD _id () {return d_id;} public void setd_id (int d_id) {this.d_id = d_id;} Public String Getd_name () {return d_name;} public void Setd_name (String d_name) {this.d_name = D_name;} Public set<project> getprojects () {return projects;} public void Setprojects (set<project> projects) {this.projects = projects;}}
2. Configuration Files

Many-to-many relationship mappings
1. Mapped Collection properties
2. Intermediate table for attributes
3. Foreign key field ProjId
4. Intermediate table Properties for foreign key fields did
5. Types of collection property elements

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

Test class:

Package Cn.itcast.many2many;import Org.hibernate.transaction;import Org.hibernate.classic.session;import Org.junit.test;import Cn.itcast.utils.hibernateutil;public class App {@Testpublic void Test () {Session session = Hibernateutil.getsession (); Transaction Transaction = Session.begintransaction ();//maintenance of the employee table//1 through the Project table. Project Table Projects Proj=new project ();p Roj.setproj_ Name ("OA Project"); Project Proj2=new project ();p roj2.setproj_name ("ERP system")//2. Employee table Developer dev_lwk=new Developer ();d ev_lwk.setd_name ("Li Weikang");D eveloper dev_hw=new Developer ();d ev_hw.setd_name ("Anlu");D eveloper  dev_ccs=new Developer ();d ev_ Ccs.setd_name ("Cheng");//3. Relationship proj.getdevelopers (). Add (DEV_LWK);p roj.getdevelopers (). Add (DEV_HW); Proj2.getdevelopers (). Add (DEV_LWK);p roj2.getdevelopers (). Add (Dev_ccs); Session.save (proj); Session.save (PROJ2); Transaction.commit (); Session.close ();}}

Run the test to get the database table:





Foreign key of T_relation


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hibernate Many2many Mapping Relationship

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.