hibernate one to one

*, Hibernate one-to-one configuration startup prerequisite

The existing entity person and entity idcard have a one-to-one relationship

 

*, when the primary key of idcard is referenced by person as a foreign key

--Add the idcard_id field to the person's hbm configuration file

<property name="idcareId" type="java.lang.Integer">
	<column name="idcare_id">
		<comment>idcard主键ID</comment>
	</column>
</property>
Note: person.setIdcardID(); is required to save the primary key of the idcard during the save operation

   

When the person needs to manage the entity of the idcard or lazily load the entity class

--Add the idcare entity attribute to the entity class of person

private IdCare idcard;
get/set

 

--Add the many-to-one attribute to the person's hbm configuration file

<many-to-one name="idcard managed entity object" class="com.idcard"
cascade="none" unique="true guarantees one-to-one key" insert="false" update="false" >
	<column name="idcared_id, the foreign key field in the person table is the idcard primary key id" not-null="true" />
</many-to-one>

 

On the contrary: you can configure the above information in the entity class of idcard and hbm. If configured at the same time, it is also called two-way one-to-one association

 

Whether it is correct or not, it still needs to be discussed, and if it is inappropriate, please let me know~

-------------------------------------------------- Small Bug--------------------------------------------------------- -------

*、Could not initialize proxy - the owning Session was closed

See: http://lbovinl.iteye.com/blog/2373492

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326537328&siteId=291194637
one
ONE