• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to map one-to-one relation

 
Raghda Ismail
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have User and Department classes ,the relation from Department to User is one to one unidirectional(Department has head ,and User can be head of only
one Department).
I did some search and found that to do this mapping i must do the following

<many-to-one
name="head"
class="helper.User"
column="department_head"
unique="true"
/>

and that unique="true" will force the relation to be one to one.

is that true??

I tried that but it works as many Departments to the same user, i add new department to user who is already a head and it works fine.

if this mapping is wrong ,could any one please tell me how to do it?

Thanks

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there's a real reason to not use a one-to-one mapping, that's the way to do it.

Here it looks like you're mapping from departments to a department head (user). That just means a department can have only one head, not that a user can't have more than one department.

To paraphrase Java Persistence in Action:

[...] you declare that a particular user can be referenced by at most one department, as a department head.

(Emphasis mine.) I translate that to mean that a department can have only one head, not that a user can't have multiple departments. But I might be misunderstanding what you perceive the symptom(s) of the problem(s) is.
 
Raghda Ismail
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi David ,

Thanks for your reply.
You did not misunderstand ,what you said is exactly what i mean:
" I translate that to mean that a department can have only one head, not that a user can't have multiple departments"

and my problem with using one to one mapping is that i am not able to fetch users eagerly with the departments
I tried using this mapping :

<one-to-one
name="head"
class="helper.User"
foreign-key="department_head" //this is the name of the foreign key column in departments table
fetch="join"
/>

is there any thing wrong with this mapping??

Thanks in advance
 
Raghda Ismail
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi every body ,

I am still having this problem.
could any body just have a look on the mapping and tell me if there is something wrong with it?

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic