-->
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.  [ 3 posts ] 
Author Message
 Post subject: Mapping relationship between 3 tables
PostPosted: Thu Jul 21, 2005 11:22 pm 
Newbie

Joined: Thu Jul 21, 2005 6:30 pm
Posts: 5
I am working on a legacy system, but I have a fair amount of flexibility to make the changes I feel are necesary. One of the projects that I am working on is converting the code base from a mix of EJB and JDBC to (mostly) hibernate. Things have been going very smooth except for one complex mapping that I'm not sure how to resolve.

The system I am working on has been designed to be highly customizable. We have one Facility table that consists of the facility contracts. Each facility is associated with exactly 1 network. So far, so good. The problem: Many of the network details are customizable per-user. With the current system, we are using *HUGE* hand coded SQL statements to handle all of the customization. The queries that we use are typically around 30 lines long, do an inner join on 4 tables (including joining a table to itself), 4 left joins and a slew of COALESCE() calls. Mapping that whole mess seems like a non-starter. Even if I could do it, I suspect that the performance hit would be significant.

My solution was to create a virtual view (since MySQL doesn't support real views and I expect to get better performance from a real table anyhow). The "view" will be rebuilt nightly or as needed. It will basically reduce the complex set of joins that we currently use down to a single table that is keyed off of User and NetworkId - reducing most of our queries down to a single inner join.

And now for the problem...

If I proceed with this plan, the database will look something like (excuse the DDL errors):

Code:
CREATE TABLE Users (
    Id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
    User VARCHAR(20) NOT NULL,
    ....   
);


Code:
CREATE TABLE Facility (
    Id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
    NetworkId INTEGER NOT NULL,
    Name VARCHAR(40) NOT NULL,
   ....
);


Code:
CREATE TABLE UserNetView (
    UserId INTEGER NOT NULL,
    NetworkId INTEGER NOT NULL,
    ... (options) ...
);
PRIMARY KEY (UserId, NetworkId);


This design will reduce the mess I am currently using to a single very ugly query that is run nightly (to update the "view") and then queries like:

Code:
SELECT {}
FROM
Facility JOIN UserNetView ON (Facility.NetworkId = UserNetView.NetworkId AND UserNetView.UserId = ?)
WHERE
...


in our functional code.

What I would like to create is a Facility object with a getNetwork() method that returns the network settings for the current user. I was hoping to just avoid using Session.load() and instead creating a series of custom queries like:

Code:
from Facility f where f.network.userId = :userId and f.id = :primkey
from Facility f where f.network.userId = :userId and f.name = :name
...etc...


I am not sure how to proceed with the mapping though. From a pure database point of view the relation between Facility and UserNetView is technically many-to-many I guess. But, if you include the User id - which I would always use - it is logically one-to-many (each facility will be associated with exactly one network entry per-user). At least, that is how I would prefer to treat the relation.

My best idea so far has been to make the relation many-to-many and then provide access functions in the Facility object to pull the network info from the Set.

One thing in my favor is that the Facility and UserNetView tables will both be read-only.

Is there a better way of handling this? Any suggestions are welcome.

Thanks,
Josh


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 4:29 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
Josh,

about many-to-many, please read Don't use exotic association mappings. from Best Practices
http://www.hibernate.org/hib_docs/v3/reference/en/html/best-practices.html

_________________
Leonid Shlyapnikov


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 11:12 am 
Newbie

Joined: Thu Jul 21, 2005 6:30 pm
Posts: 5
shl wrote:
Josh,

about many-to-many, please read Don't use exotic association mappings. from Best Practices
http://www.hibernate.org/hib_docs/v3/reference/en/html/best-practices.html


Hey Leonid.

Thanks for the link. The problem is that the paragraph you are referring to is basically trying to politely say, "Normalize your damn database properly!". While normally I would agree with that, I am actually in a situation where the "view" I am dealing with is a conscious de-normalization of a very complex database structure. The goal is to drastically simplify the code and, as a bonus, hopefully see some performance gains. While this makes the hibernate mapping less obvious, it really does make everything else in our system much easier. Our tools for managing the user settings still get to access the properly normalized database (with minor tweaks to rebuild the view) while the actual application will get to use the new "view" - which will be *much* less complicated.

I have pretty much given up on sanely mapping the relationship between these tables. I am going to play with hibernate's Theta-style join (Hibernate in Action - section 7.3.5) functionality and if that doesn't work I will try to use the natural SQL functionality in hibernate. If I can't make that work... Well, I guess it's back to plain ole' JDBC and manually mapping the ResultSet into objects.

Thanks,
Josh


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