-->
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.  [ 5 posts ] 
Author Message
 Post subject: one-to-one Mapping Example or Guidance
PostPosted: Mon Apr 10, 2006 3:14 pm 
Newbie

Joined: Mon Apr 10, 2006 2:58 pm
Posts: 8
I have 2 tables that use the same primary key to link each other on a one-to-one basis.

Can someone point me to an example of such a situation that includes what the Java class files look like and the mapping files. I've found a lot of examples, but they tend to show just the mapping file and I don't know how my properties should be configured.

Alternatively, can you tell me how the following should be done:

The tables are as follows:



MYSESSION
=======
sessionId VARCHAR(32) PK
description VARCHAR(255)


INBOUND_SESSION
===============
sessionId VARCHAR(32) PK
startTime TIMESTAMP



for the inbound session, I'm not sure if the class should look like this (I left off the setters and getters for readability:

public class InboundSession {
private String sessionId ; // Use the session ID
private Date timestamp ;
}

or like this:

public class InboundSession {
private MySession session ; / Use a MySession
private Date timestamp ;
}



I'm pretty sure MySession should look like this:

public class MySession {
private String sessionId ;
private String description ;
private InboundSession inboundSession ;
}

Even though it's a one-to-one relationship, I want the sessionId to be a uuid generator on the MySession object and then that sessionId to be used for the InboundSession object.

I've tried various combinations and settings and I get one of two possible errors:

- Hibernate can't process the hbm.xml file
- I get an error about assigning a null id in a one-to-one mapping


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 11, 2006 1:14 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The field representing a one-to-one (or any entity association) should always be an object, not a value. So InboundSession.getMySession() needs to return a MySession object, not MySession's id. InboundSession will have an id field, but you should probably not include getId or setId in its interface (though hibernate needs the methods, so they should be in the implementing class).

MySession will have a getSessionId() (returns String) and a getInboundSession() (returns an InboundSession object).

The id generator on InboundSession should be class="foreign". Everything else is pretty standard, nothing special needs to be done.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 11, 2006 8:37 am 
Newbie

Joined: Mon Apr 10, 2006 2:58 pm
Posts: 8
Thanks for clarifying this. It was the exact info I needed to get over the hurdle.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 12:56 pm 
Newbie

Joined: Sun May 22, 2005 4:21 pm
Posts: 10
Hi, I have the same problem and I am struggling with how the above example can be done with xdoclet tags.

My example is similar to the MySession one except the id fields are java.lang.Long fields.
I am guessing that the xdoclet tags in MySession would look like this:
Code:
   /**
    * @return Returns the sessionId.
    * @hibernate.id generator-class="increment" not-null="true"
    *               column="sessionId"
    */
   public Long getSessionId() {
      return sessionId
   }

   /**
    * @hibernate.one-to-one class="InboundSession"
    */
   public InboundSession getInboundSession() {
      return inboundSession;
   }


and in InboundSession:
Code:
   /**
    * no tags here??
    */
   public Long getId() {
      return id;
   }

   /**
    * @hibernate.one-to-one class="MySession" constrained="true"
    * @hibernate.id generator-class="foreign" not-null="true" column="sessionId"
    * @hibernate.generator-param name="property" value="sessionId"   
    */
   public MySession getMySession() {
      return mySession;
   }


However, when I create the hibernate mapping files, I come up with an InboundSession table that has a composite id made up of all the fields in the MySession table, which is not what I want.

Can anyone help me?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 3:48 pm 
Newbie

Joined: Sun May 22, 2005 4:21 pm
Posts: 10
Never mind, I got it. If anyone is interested, the mapping in the Inbound Session is like this:

Code:
   /**
    * @hibernate.id generator-class="foreign" not-null="true" column="sessionId"
    * @hibernate.generator-param name="property" value="mySession"
    */
   public Long getId() {
      return id;
   }

   /**
    * @hibernate.one-to-one class="MySession" constrained="true"   
    */
   public MySession getMySession() {
      return mySession;
   }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.