-->
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: Mapping aggregates and handling non-existent relations...
PostPosted: Wed Dec 24, 2003 2:18 pm 
Newbie

Joined: Sat Dec 06, 2003 12:57 am
Posts: 3
Location: Richmond, Virginia, USA
Howdy,

I just have two general questions, as I've been able to work through most of my issues by reading this fabulous resource. And actually, I have a plan to address my first question (mapping aggregates).

Background:
To get some experience with Hibernate, I'm attempting to map a bugzilla database I use at work. For the most part I've been able to get model objects mapped to the underlying database.

Issues:
I have two model objects called Bug and Vote. Vote has a many to one relationship with Bug. Bug has a 'votes' property. I really don't have to model this relationship since the database stores the number of votes for the Bug. However, if I needed to support this relationship, how would I? The approach I thought of involved defining a one-to-many relationship from Bug to Vote and calculate the sum in the Bug model object. Is there a way to map a relationship from a model object to an aggregate of another model object? In this case "bug.votes = select sum(count) from votes where bug_id = ?"

The other issue is a little trickier. Using my Bug model object from above, it has a many to one relationship with another model object called Profile. Each Bug can have a QA Contact. I have that mapped correctly. The problem I've run into, though, is a QA Contact is not required and Bugzilla stores a 0 in that column; there is no Profile with the id 0. So my question is, how can I support a many to one relationship when the one may not exist? Is there a way for me to intercept the process of fulfilling this relationship and examing the id /before/ an attempt to retrieve it is made? For now, I'm not supporting/implementing the QA Contact relationship; I can't continue that though.

My thanks and appreciation to the Hibernate team. It's been a blast learning and using this wonderful tool.

- Mel Riffe


Top
 Profile  
 
 Post subject: Re: Mapping aggregates and handling non-existent relations..
PostPosted: Mon Dec 29, 2003 6:22 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
juicyparts wrote:
Is there a way to map a relationship from a model object to an aggregate of another model object? In this case "bug.votes = select sum(count) from votes where bug_id = ?"

I don't think so. Why don't you just map the count column of Vote ?

juicyparts wrote:
So my question is, how can I support a many to one relationship when the one may not exist? Is there a way for me to intercept the process of fulfilling this relationship and examing the id /before/ an attempt to retrieve it is made?

Hum, hard. The bugzilla db sucks.
Implement the Lifecycle interface on the QA stuff and vetoe every save or update.
Proxy the Profile object. id request will not load it. then made a "public" accessor different from the mapped one. It will return the Profile if mapped_profile.id != 0).

I'm not very satisfied ot that solution however.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: Mapping aggregates and handling non-existent relations..
PostPosted: Fri Jan 02, 2004 12:01 am 
Newbie

Joined: Sat Dec 06, 2003 12:57 am
Posts: 3
Location: Richmond, Virginia, USA
epbernard wrote:
juicyparts wrote:
Is there a way to map a relationship from a model object to an aggregate of another model object? In this case "bug.votes = select sum(count) from votes where bug_id = ?"

I don't think so. Why don't you just map the count column of Vote ?


For this specific instance, I am mapping the count column. I guess I was asking more generally about mapping aggregates. I'm sure it's an exotic condition and isn't requested. No worries, though.

epbernard wrote:
juicyparts wrote:
So my question is, how can I support a many to one relationship when the one may not exist? Is there a way for me to intercept the process of fulfilling this relationship and examing the id /before/ an attempt to retrieve it is made?

Hum, hard. The bugzilla db sucks.
Implement the Lifecycle interface on the QA stuff and vetoe every save or update.
Proxy the Profile object. id request will not load it. then made a "public" accessor different from the mapped one. It will return the Profile if mapped_profile.id != 0).

I'm not very satisfied ot that solution however.


I don't totally understand your suggestion. Is there some documentation that covers this? It sounds interesting and I'd like to pursue it. I just need to be pointed in the right direction...

And I totally agree - The bugzilla db sucks! Definitely not implemented in the ways I've been taught. ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2004 9:29 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
http://www.hibernate.org/hib_docs/reference/html_single/#persistent-classes-s3 for the vetoe stuff + the wiki section I guess.

Then the public private stuff:
map a property with private getters and setters.

Add a POJO property with public getters and setters.

Code:
/**
  * not mapped property
  */
public Profile getProfile() {
  if (getMappedProfile().id == 0) {
    return null;
  } else {
     return getMappedProfile()
  }
}

/**
  * @hibernate.many-to-one ... (or sthg like that)
  */
private Profile getMappedProfile() {
  return _mappedProfile;
}

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 12:45 am 
Newbie

Joined: Sat Dec 06, 2003 12:57 am
Posts: 3
Location: Richmond, Virginia, USA
Thanks Emmanuel. I will study your suggestion!

- Mel Riffe


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.