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.