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: mapping "optinal" one-to-one relationship
PostPosted: Tue Sep 18, 2007 8:24 pm 
Newbie

Joined: Sun Nov 20, 2005 9:49 pm
Posts: 3
Location: Sydney, Australia
Hi All, I'm using Hibernate 3.2.0 and I have the following problem relating to mapping two entities:

USER:
- username (PK)

INDIVIDUAL:
- id (PK)
- username (FK)


Basically, a USER can map to 0 to 1 INDIVIDUALs, and an INDIVIDUAL can map to 0 to 1 USERs. So basically it's an optional one-to-one relationship (is that makes sense).

I've managed to map this as follows:

User.hbm.xml:
Code:
<hibernate-mapping>
  <class name="com.test.User" table="USER">
    <id name="username" column="USERNAME">
      <generator class="assigned" />
    </id>

    <one-to-one name="individual" property-ref="username"/>
  </class>
</hibernate-mapping>


Individual.hbm.xml:
Code:
<hibernate-mapping>
  <class name="com.test.Individual" table="INDIVIDUAL">
    <id name="id" type="long" column="ID">
      <generator class="sequence"/>
    </id>

    <property name="username" column="USERNAME" />

    <!--  actually a one-to-one association due to "unique=true" -->
    <many-to-one name="user" column="username" unique="true" not-null="false"/>
  </class>
</hibernate-mapping>


And here are my domain models:

Code:
public class User {
  private String username;
  private Individual individual;
 
  /* getters and setters here */
}

public class Individual {
  private Long id;
  private String username;
  private User user;
 
  /* getters and setters here */
}


This works, but I find it a little unsafe having to expose "username" in my Individual.hbm.xml mapping and Individual.java. I'd rather keep that hidden in the Java side of things since it's a foreign key.

Any ideas? I've tried to implement a many-to-many mapping on both entities with unique="true" and not-found="ignore" options, but this keeps throwing errors.

Thanks in advance for any help you can give.

_________________
~gautam


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 21, 2007 1:27 am 
Newbie

Joined: Sun Nov 20, 2005 9:49 pm
Posts: 3
Location: Sydney, Australia
anyone ?

_________________
~gautam


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.