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
Hibernate allows you to work with persistent XML data in much the same way you work with persistent POJOs. A parsed XML tree can be thought of as another way of representing the relational data at the object level, instead of POJOs. Hibernate supports dom4j as API for manipulating XML trees.
The way we do it in code is with @OneToMany. Let's map the Cart class to the collection of Item objects in a way that reflects the relationship in the database: public class Cart { //... @OneToMany(mappedBy="cart") private Set<Item> items; //... }
@OneToOne annotation marks the relationship between two entities with one-to-one multiplicity. It is not normally necessary to specify the associated target entity explicitly since it can usually be inferred from the type of the object being referenced.
The <property> element is used to map a Java class property to a column in the database table. The name attribute of the element refers to the property in the class and the column attribute refers to the column in the database table.
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With