hibernate5.x the relationship between table and table operation code implementation

Source: Internet
Author: User
Tags set set custom name

One or one-to-many operations (accounts and contacts)

1, one-to-many basic implementation steps

The first step is to create two entity classes, accounts and contacts

The second step allows two entity classes to represent each other

(1) Representing multiple contacts within the client entity class

-A client with multiple contacts

(2) representing the customer in the Contact entity class

-a contact can only belong to one customer

The third step configures the mapping relationship

(1) A common entity class corresponds to a mapping file

(2) Complete the basic configuration of the map

(3) In the mapping file, configure a one-to-many relationship

-In the customer mapping file, all contacts are represented

1 <?XML version= "1.0" encoding= "UTF-8"?>2 3 <!--hibernate Getting Started setting up hibernate environment The first step to importing hibernate jar packages -4 <!--mapping configuration file DTD constraints -5 <!DOCTYPE hibernate-mapping Public6 "-//hibernate/hibernate Mapping DTD 3.0//en"7 "Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">8     <hibernate-mapping>9     <classname= "Com.cn.entity.Custom"Table= "T_custom">Ten     <IDname= "CID"column= "CID"> One     <Generatorclass= "Native"></Generator> A     </ID> -     < Propertyname= "CustName"column= "CustName"></ Property> -     < Propertyname= "Custlevel"column= "Custlevel"></ Property> the     < Propertyname= "Custsource"column= "Custsource"></ Property> -     < Propertyname= "Custphone"column= "Custphone"></ Property> -     < Propertyname= "Mobile"column= "Mobile"></ Property> -     <!--In the customer mapping file, all contacts are represented and all contacts are represented using the set label + The set tag has the name attribute: The property value is written in the client entity class to represent the contact's set set of names - -     <!--Cascade Properties: Cascade add cascade Delete - +     <!--batch-size: Higher values send fewer statements - A     <Setname= "Setlinkman"Cascade= "Save-update,delete"batch-size= "Ten"> at     <!--one-to-many-built table with foreign keys - hibernate mechanism: The foreign key is maintained in two directions, and the Foreign key column property value is configured on one and more sides: foreign Key Name - -     <Keycolumn= "Clid"></Key> -     <!--client all contacts, class inside write Contact entity class full path - -     <One-to-manyclass= "Com.cn.entity.LinkMan"/> -     </Set> in     </class> -     </hibernate-mapping>

-In the contact mapping file, indicates the owning customer

1 <?XML version= "1.0" encoding= "UTF-8"?>2 3 <!--hibernate Getting Started setting up hibernate environment The first step to importing hibernate jar packages -4 <!--mapping configuration file DTD constraints -5 <!--It is best to have one mapping file for each entity class -6 <!DOCTYPE hibernate-mapping Public7 "-//hibernate/hibernate Mapping DTD 3.0//en"8 "Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">9     <hibernate-mapping>Ten     <classname= "Com.cn.entity.LinkMan"Table= "T_linkman"> One     <IDname= "lkm_id"column= "lkm_id"> A     <Generatorclass= "Native"></Generator> -     </ID> -     < Propertyname= "Lkm_name"column= "Lkm_name"></ Property> the     < Propertyname= "Lkm_gender"column= "Lkm_gender"></ Property> -     < Propertyname= "Lkm_phone"column= "Lkm_phone"></ Property> -  -     <!--indicates that the contact belongs to the Customer Name property: Because the custom object is used in the contact entity class, write the custom name + Class Property: Custom Full Path Column property: foreign Key Name - -     <Many-to-onename= "Custom"class= "Com.cn.entity.Custom"column= "Clid"></Many-to-one> +     </class> A     </hibernate-mapping>

The fourth step is to create the core configuration file and introduce the mapping file into the core configuration file.

2, a pair of multi-cascade operation

1 Cascade Save : Add a customer to add multiple contacts to this customer

2 Cascade Delete: Delete a customer, all the contacts inside this customer are also deleted

(1) Add a customer, add a contact for this customer

① Complex notation:

Cascading add @testpublic void TestAdd1 () {sessionfactory sessionfactory = null; Session session = NULL; Transaction tx = null;try {sessionfactory = Dutils.getsessionfactory (); session = Dutils.getsessionobject ();//Open Transaction tx = Session.begintransaction ();//create customer and contact Object Custom custom = new custom (); Custom.setcustname ("NetEase"); Custom.setcustlevel (" VIP "), Custom.setcustsource (" network communication "), Custom.setcustphone (" the ") Custom.setmobile (" 333 "); Linkman Linkman = new Linkman () linkman.setlkm_name ("Mary"); Linkman.setlkm_gender ("female"); Linkman.setlkm_phone ("8888" //2 in the customer indicates that all contacts represent the customer in the contact//Establish customer object and contact object Relationship//2.1 put the contact object into the set collection of the client Object Custom.getsetlinkman (). Add (Linkman);//2.2 Put the client object into the contact Linkman.setcustom (custom),//3 save to the database Session.save (custom); Session.save (linkman);//COMMIT Transaction Tx.commit ();} catch (Exception e) {e.printstacktrace ();//ROLLBACK TRANSACTION tx.rollback ();}}

② Simplified notation

-Generally add contacts based on customer

The first step is configured in the customer mapping file

-Configure the SET tag in the customer mapping file

The second step is to create the customer and contact object, only need to put the contact in the customer, you can finally just save the customer.

2. Delete a customer, delete all the contacts inside the customer

The first step is to configure the set tag in the customer mapping file

(1) Using attribute Cascade property Value Delete

The second step is to delete the customer directly in the code

(1) Query object according to ID, call session inside Delete method deletes

Step three execution process

(1) query customer by ID

(2) Query contact based on foreign key ID value

(3) Set the contact foreign key to NULL

(4) Delete contacts and Accounts

Two, many-to-many operations (users and roles)

hibernate5.x the relationship between table and table operation code implementation

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.