-->
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.  [ 7 posts ] 
Author Message
 Post subject: How to make a referance to a single other mapped object?
PostPosted: Mon Sep 08, 2003 7:00 am 
Beginner
Beginner

Joined: Mon Sep 08, 2003 6:52 am
Posts: 46
Hiya, I've started using Hibernate recently, and think it's great.
I'm using it for maps and sets and whatnot, and don't have any problems with that. I have also used the special one-to-one relationship, where it is appropriate. However, there is something that I am trying to do that seems like it is totally trivial, but I can't figure out how to do it.

I have a class that looks something like this:

class Thing
{
public Widget getPrimaryWidget() {}
public void setPrimaryWidget(Widget widget) {}

public Map getWidgets()
public void setWidgets(Map widgets)

....
}

The problem I am having is with the primaryWidget field. I cannot figure out how to map it in the config file properly.

A one-to-one relationship is not appropriate, since the primary widget guy can change, and won't have the same primary key as the Thing guy.

In the database I have a field that represents the primaryWidgetId, but for the moment I have to use it only as an id, and then load up the object seperately.

How can I do this? Can I just use a property element and set the type to the other mapped type, or...?

Thanks,

Hunter


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 7:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
What you are looking to do is termed a many-to-one relationship. Check out the docs on setting this kind of relationship up.

http://www.hibernate.org/hib_docs/reference/html/or-mapping.html#or-mapping-s1-8


Top
 Profile  
 
 Post subject: But it's not many to one...
PostPosted: Mon Sep 08, 2003 7:37 am 
Beginner
Beginner

Joined: Mon Sep 08, 2003 6:52 am
Posts: 46
I've set up many to one relationships before, but I didn't use it here
because it's not actually many-to-one. Realistically, it is like a one to one relationship, but it's not strictly one-to-one.

Are you saying that it's okay to use many-to-one even though that's not really what it means?

H


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 8:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
From what you described, I am assuming your table structure is something like:

THING
------------
THING_ID
PRIMARY_WIDGET_ID
...

WIDGET
-------------
WIDGET_ID
....

THING_WIDGET_JT
---------------------
THING_ID
WIDGET_ID

Correct?

Then this is a one-to-many relationship from a relational perpective, whether you think of it that way or not. In this setup a Widget maybe defined as the primary widget for multiple things....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 9:24 am 
Beginner
Beginner

Joined: Mon Sep 08, 2003 6:52 am
Posts: 46
[quote="steve"]From what you described, I am assuming your table structure is something like:

THING
------------
THING_ID
PRIMARY_WIDGET_ID
...

WIDGET
-------------
WIDGET_ID
....

THING_WIDGET_JT
---------------------
THING_ID
WIDGET_ID

Correct?

Then this [b]is[/b] a one-to-many relationship from a relational perpective, whether you think of it that way or not. In this setup a Widget maybe defined as the primary widget for multiple things....[/quote]

Actually, THING is correct but WIDGET is:

WIDGET
------------
WIDGET_ID
THING_ID

and there is no join table.


I've started to use the many-to-one, and it pretty much does the trick, even though it looks funny. From my point of view, many-to-one is used to map a child back to a parent, but in this instance THING is the parent as well as holding a ref to a particular child.

Now the problem I'm having is a problem chicken-and-egg problem with id's for the THING and the child WIDGETS. I think I'm going to have to save everything, do an explicit flush, and the set the PrimaryWidget on the thing afterwards.

Thanks for the speedy responses, btw!! Hibernate is one of the best developer tools I've seen in years.


Top
 Profile  
 
 Post subject: it works better when you do it correctly...
PostPosted: Mon Sep 08, 2003 9:47 am 
Beginner
Beginner

Joined: Mon Sep 08, 2003 6:52 am
Posts: 46
Actually My chicken and egg problem went away, I had some incorrect values in my mapping file.

thanks for the help!!

H


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 1:26 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
I have almost the same thing in one of my projects where a parent logically mapps to a collection of children (by a foreign key on the child table) but then also holds a reference to a particular child (by a foreign key to the child on the parent table).

The only thing to be aware of is creation of a parent, if you are using proper referential integrity constraints. I basically needed to script this as a creation of the parent (with cascade of the children collection), followed by setting the initial "particular" child and then an update of the parent

FYI


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