问题
I have a user table and a user_detail
table with one to one mapping user_detail
table have a field user_id
to be used for this relation which stores id field value of corresponding user.
How to write the hibernate hbm
file for this relation?
UPDATE
what my problem is that user's primary key is id
, user_detail's foreign key is user_id
all the examples i got in internet users user_id as users primary key and the same as foreign key in other table
回答1:
For User mapping....
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 25 April 2011 7:52:33 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.rais.User" table="USER" catalog="mydb">
<id name="userId" type="java.lang.Integer">
<column name="USER_ID" />
<generator class="identity" />
</id>
<property name="userName" type="string">
<column name="USER_NAME" length="10" not-null="true" unique="true" />
</property>
<property name="userCode" type="string">
<column name="USER_CODE" length="20" not-null="true" unique="true" />
</property>
<one-to-one name="userDetail" class="com.rais.UserDetail"
cascade="save-update"></one-to-one>
</class>
</hibernate-mapping>
For UserDetail mapping.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 25 April 2011 7:52:33 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.rais.UserDetail" table="USER_DETAIL"
catalog="mydb">
<id name="userId" type="java.lang.Integer">
<column name="USER_ID" />
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<one-to-one name="user" class="com.rais.User"
constrained="true"></one-to-one>
<property name="compName" type="string">
<column name="COMP_NAME" length="100" not-null="true" />
</property>
<property name="compDesc" type="string">
<column name="COMP_DESC" not-null="true" />
</property>
<property name="remark" type="string">
<column name="REMARK" not-null="true" />
</property>
</class>
</hibernate-mapping>
回答2:
In the User.hbm you need to declare the relation with UserDetails like this:
<one-to-one name="user_detail" foreign-key="user_id" class="UserDetail full name "/>
Hope this help you
回答3:
For one-to-one associations where primary key of UserDetail is not the foreign key, we have to represent it as many-to-one association on the owning side.
From Hibernate docs https://docs.jboss.org/hibernate/orm/3.2/reference/en/html/mapping.html
Alternatively, a foreign key with a unique constraint, from Employee to Person, may be expressed as:
<many-to-one name="person" class="Person" column="PERSON_ID" unique="true"/>
And this association may be made bidirectional by adding the following to the Person mapping:
<one-to-one name"employee" class="Employee" property-ref="person"/>
So, there is no way to map this association using one-to-one, you have to change the mapping to many-to-one on the owning side (Table holding the foreign key).
回答4:
<one-to-one name="user_detail" class="give the full specified className with package declaration" cascade="save-update"></one-to-one>
Here is an good example
http://www.mkyong.com/hibernate/hibernate-one-to-one-relationship-example/
please find it.
来源:https://stackoverflow.com/questions/14682169/hibernate-one-to-one-mapping-with-a-reference-column-xml-mapping