Hibernate establishes one-to-one association, and hibernate multiple-to-one Association

Source: Internet
Author: User

Hibernate establishes one-to-one association, and hibernate multiple-to-one Association

The following content is organized from the second edition of Hibernate

Ing code:

<span style="font-size:18px;"><many-to-one        name="customer"        column="CUSTOMER_ID"        class="mypack.Customer"        not-null="true"        lazy="false"/></span>

The <role-to-one> element establishes a customer ing between the customer attribute and the foreign key mermer_id of the ORDERS table. It includes the following attributes. Name: Set the name of the property of the persistence class to be mapped. Here it is the customer property of the Order class.
Column: Set the table foreign key corresponding to the persistence class attribute. Here it is the foreign key CUSTOMER_ID of the ORDERS table. Class: Set the property type of the persistent class to be mapped. Here, set the customer property to the Customer type. Not-null: true indicates that the customer attribute cannot be null. The default value of this attribute is false. The not-null attribute affects the database schema generated by the hbm2ddl tool. The hbm2ddl tool sets the not null constraint for the CUSTOMER_ID foreign key of the ORDERS table, however, the not-null attribute does not affect the Java source code generated by hbm2java. In addition, the not-null attribute affects the runtime behavior of Hibernate. When Hibernate saves the Order object to the database, it first checks whether its customer attribute is null. Lazy: if it is a proxy, it indicates that a delay retrieval policy is used for the associated Customer object and a proxy is used. This is the default value. If it is false, it means that when Hibernate loads the Order object from the database, it will automatically load the Customer object associated with it immediately.
Note: When Hibernate persists a temporary object (created through the new statement), it will not automatically persist other temporary objects associated with it by default, so it will throw a TransientObjectException exception. To enable automatic persistence of the associated temporary object when Hibernate persists a temporary object, you can set the cascade attribute of <export-to-one> to "save-update ", the default value of the cascade attribute is "none ". When the cascade attribute is "save-update", it indicates that when the current object is saved or updated (that is, when the insert or update statement is executed), the objects associated with it are saved or updated in cascade mode.

Related Article

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.