-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: how to write an one-to-one mapping xml?
PostPosted: Wed Jul 12, 2006 11:38 am 
Newbie

Joined: Sat Mar 18, 2006 1:27 am
Posts: 15
okay, here is the problem. i have two tables user and userprofile.
in user table, i have the basic info and in userprofile table, i have more specific info.
they have the same pk as uid.

now i wanna be like when i access the user table, i only get the info from the user table. on the other hand, when i access the userprofile table, it comes w/ the info in the user table as well.

how do i write the mapping xml file and besides that, do i have to make any change in the DB?

plz help. thanx


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 12:44 pm 
Newbie

Joined: Wed Oct 12, 2005 11:17 am
Posts: 7
From the Hibernate Reference Documentation version 3.0.5 PDF, Section 8.4.2:

Quote:
A bidirectional one-to-one association on a primary key uses the special id generator.
Code:
<class name="Person">
  <id name="id" column="personId">
    <generator class="native"/>
  </id>
  <one-to-one name="address"/>
</class>

<class name="Address">
  <id name="id" column="personId">
    <generator class="foreign">
      <param name="property">person</param>
    </generator>
  </id>
  <one-to-one name="person" constrained="true"/>
</class>



In your case, it would probably be like this:

Code:
<class name="User">
  <id name="id" column="userId">
    <generator class="native"/>
  </id>
  <one-to-one name="userProfile"/>
</class>

<class name="UserProfile">
  <id name="id" column="userId">
    <generator class="foreign">
      <param name="property">user</param>
    </generator>
  </id>
  <one-to-one name="user" constrained="true"/>
</class>


Note, because of your choice in table names, you may want to use backtics in your mapping properties so that keywords are properly enclosed. I don't know many databases that don't have USER as a keyword. For instance, instead of
Code:
<class name="User" table="USER" schema="TEST">

it would be better to use
Code:
<class name="User" table="`USER`" schema="TEST">

That way the queries will surround the table name USER with double quotes or brackets, as necessitated by your database.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.