Hibernate mapping Relationship-relational mapping (one-to-many)

Source: Internet
Author: User

A one-to-many association relationship is the most commonly used relational relationship in a database table relationship, and there are many examples, such as the relationship between customer and order, to learn about a pair of implementations in hibernate.
1. Database design

CREATE TABLE ' Customer '(' CID 'bigint -) not NULLAuto_increment,' name ' varchar( -) not NULL,' phone ' varchar( One) not NULL,' Address ' varchar( the)CHARACTER SETUtf8COLLATEUtf8_bin not NULL,' Register_time ' timestamp  not NULL DEFAULT Current_timestamp  on UPDATE Current_timestamp,PRIMARY KEY(' CID '),UNIQUE KEY ' Ak_name '(' name ')) Engine=innodb auto_increment=6 DEFAULTCharset=utf8;CREATE TABLE ' orders '(' OID 'bigint -) not NULLAuto_increment,' CID 'bigint -) not NULL,' Price ' Double  not NULL,' name ' varchar( the)CHARACTER SETUtf8COLLATEUtf8_bin not NULL,PRIMARY KEY(' OID '),KEY ' Fk_cid '(' CID '),CONSTRAINT ' Fk_cid ' FOREIGN KEY(' CID ')REFERENCES ' Customer '(' CID ')) Engine=innodb auto_increment= A DEFAULTCharset=utf8;

2. Configuration files

"COM.STUDY.HIBERNATE.HBM"> <class name="Customer"table="Customer"catalog="Hibernate"> <ID name="CID"Type="Java.lang.Long"> <columnname="CID"/> <generatorclass="Identity"/> </ID> < Property name="Name"Type="string"> <columnname="Name" length=" the"  not-null="true"Unique="true"/> </ Property> < Property name="Phone"Type="string"> <columnname="Phone" length="One"  not-null="true"/> </ Property> < Property name="Address"Type="string"> <columnname="Address" length=" the"  not-null="true"/> </ Property> < Property name="Registertime"Type="Timestamp"> <columnname="Register_time" length="very"  not-null="true"/> </ Property> <Set name="Orderses"table="Orders"Inverse="true"lazy="true"Fetch="SELECT"> <key> <columnname="CID"  not-null="true"/> </key> <one- to-manyclass="Orders"/> </Set> </class>"COM.STUDY.HIBERNATE.HBM"> <class name="Orders"table="Orders"catalog="Hibernate"> <ID name="OID"Type="Java.lang.Long"> <columnname="OID"/> <generatorclass="Identity"/> </ID> <many- to-onename="Customer" class="Customer"Fetch="SELECT"> <columnname="CID"  not-null="true"/> </many- to-one> < Property name="Price"Type="Double"> <columnname="Price"precision="a"scale="0"  not-null="true"/> </ Property> < Property name="Name"Type="string"> <columnname="Name" length=" the"  not-null="true"/> </ Property> </class>

3. Persistent class

 Public  class Customer implements Java. io. Serializable { PrivateLong CID;PrivateString name;PrivateString phone;PrivateString address;PrivateDate Registertime;PrivateSet orderses =NewHashSet (0);} Public  class Orders implements Java. io. Serializable { PrivateLong OID;PrivateCustomer customer;Private DoublePricePrivateString name;}

4, Key summary
Lazy loading aspect
1) use lazy and fetch two fields to configure lazy loading, where lazy loading is not the lazy load of load, but the delayed loading of associated objects
2) lazy= False is valid for createquery and get operations, Fetch=join is only valid for operations of Get mode
3) When fetch=join, if the surface level of the connection is too deep, it may affect performance, This allows you to set the Max_fetch_depth property in the Hibernate properties file to control the number of levels of the connection.
4) Non-lazy loading, when batch-size is not set, when using Find lookup Customer,order, each customer will issue an SQL statement to find, if set batch-size, It optimizes the SQL lookup of orders as in, which reduces the number of bars in the SQL statement, which can only be used in the configuration of one side, that is, in a collection configuration.

Cascade operational Aspects
1) One end of Save can be saved separately, you can also cascade to save a multi-terminal data, just set cascade properties of Save-update or above; you need to be aware of the reasons for foreign key constraints, Multi-port storage at the end of either the database has a corresponding record, or multi-cascade to save one end, cascade configuration is also very simple, only need to configure cascade properties of Save-update or above
2) because the foreign key constrains one end of delete, you must set the Cascade property of the delete operation Cascade=all or above; multi-port Delete generally does not need cascade processing, just delete itself on the OK
3) Update the end of the data at the same time When updating a multi-terminal, if only the basic information has no effect on one end, if you update the end of the association, you need to update the new one end (if the new end has a corresponding multi-terminal, but also need to update the multi-terminal) that is at least 2 update statements, The other update statements are based on the corresponding multi-terminal data record on the new end; there is one more case to note that if you need to remove a multi-multiport object from the database and then update one end, you need to set the Cascade=all-delete-orphan , otherwise it will not cascade delete
4) When Casecade=all, it is the associated operation that supports Save-update and delete. Cascade=all-delete-orphan is generally used in set, where the role is to automatically delete child objects that are not part of the parent object, and also to support cascading deletions and cascading save updates, such as the following code, which deletes the order record when it is committed. Customer.getorders (). Removre (Order), Order.setcustomer (null), Tx.commit (),

Inverse
Inverse means reversing, reversing what, here is the right to reverse the maintenance of relationship changes, if the inverse=true is to maintain the relationship between the change of the right reversal, to the other side to deal with, The general one side does not maintain the association relations all is to make the many end to maintain namely sets Inverse=true, in Many-to-one inverse does not support the configuration, the default namely handles the correlation relation the change.
For example, when inverse=true, add order to customer and save customer, there will be only insert statement, Inverse=false, it means the change of the Sensing object Update relationship, When you add an order to customer and then save the customer, there is not only an INSERT statement, but also an UPDATE statement, which is obviously superfluous at this time of the update.

Unidirectional and bidirectional
One-way and two-way generally should be based on business needs to set up, one-way is nothing more than a two-way basis to remove one end of the relationship between the configuration, so that become one-way association, the specific will not elaborate.

Hibernate mapping Relationship-relational mapping (one-to-many)

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.