Hibernate bidirectional multi-pair multi-object Relational model mapping

Source: Internet
Author: User

1 bidirectional many-to-many

Business model:

Describe employees and projects

One employee can participate in multiple projects at the same time

Multiple employees can be included in a project

Analysis: The data model of the database, through the intermediate relational table, set up two one-to-many composition Many-to-many

1 Create TableEMP2 (3Enoint Primary KeyAuto_increment,4Enamevarchar5 );6 Create TableProject7 (8Pidint Primary KeyAuto_increment,9PNamevarchar,Ten ); One Create TableRelationship A ( -Enoint, -Pidint, the);
1.1 Describing the Java data Model

1.2 mapping configuration for Pojo

1.3 Loading a configuration file produces a database model

The database is a two primary foreign key.

1.4 for CRUD operations1.4.1 Adding employee information
1 @Test2  Public voidtestsaveemp ()3 {4Session session =hibernatesessionfactory.getsession ();5     //Open Transaction6Transaction TR =session.begintransaction ();7     //Create an Employee object8EMP emp =NewEmp ();9Emp.setename ("Chairman Mao");Ten Session.save (EMP); One tr.commit (); A session.close (); -}
1.4.2 Adding project information
1 @Test2  Public voidTestsaveproject ()3 {4Session session =hibernatesessionfactory.getsession ();5     //Open Transaction6Transaction TR =session.begintransaction ();7     //Create an Employee object8Project p =NewProject ();9P.setpname ("Arrival");Ten Session.save (p); One tr.commit (); A session.close (); -}
1.4.3 Add a project and assign an employee

New Add project, assign project to existing employee

1 /**2 * Add Project data to assign an existing employee3  * ****/4 @Test5  Public voidsaveprojectemp ()6 {7     //Get Session8Session session=sf.opensession ();9     //Open TransactionTenTransaction tr=session.begintransaction (); One     //Create a Project object AProject p=NewProject (); -P.setpname ("CRM"); -     //querying an employee's object theList<emp> elist = Session.createcriteria (Emp.class). List (); -     //assign an employee to a new project: Insert data into an intermediate table -P.setemps (NewHashSet (Elist)); - Session.save (p); +     //Commit a transaction - tr.commit (); +     //Freeing Resources A session.close (); at}
1.4.4 The relationship between employees and projects

The relationship is removed through the employee.

1 /***2 * Release of employee and project relationships3 * Delete data from the intermediate table4  * ***/5 @Test6  Public voiddeleteprojectemp ()7 {8     //Get Session9Session session=sf.opensession ();Ten     //Open Transaction OneTransaction tr=session.begintransaction (); A     //Querying Employee Objects -EMP emp = (EMP) session.get (EMP).class, 1); -     //get employee participation in all projects theSet<project> pros=Emp.getpros (); - System.out.println (pros); -     //Querying Project Objects -Project p= (Project) Session.get (project).class, 3); + System.out.println (p); -     /**** +      * A * The address of the element in the set set and a single query get the address of the project object, all the project objects in the Set collection and at * Single Query Gets the item object, is the same object, so Pros.remove (p), delete the address and P-like object in the collection -      * -      * *****/ -     //p.equals (obj); -     //remove an item from pros -     /*** in * The relationship is now disconnected by the employee (default inverse=false) - * Many-to-many This situation: Save the inverse=true of one side to      * ***/ +Pros.remove (P);// -     //Commit a transaction the tr.commit (); *     //Freeing Resources $ session.close ();Panax Notoginseng}

Hibernate bidirectional multi-pair multi-object Relational model mapping

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.