-->
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.  [ 6 posts ] 
Author Message
 Post subject: Complicated Hibernate Mapping - seeking design advice
PostPosted: Thu Dec 08, 2005 12:23 pm 
Newbie

Joined: Thu Dec 08, 2005 12:19 pm
Posts: 6
I am using Hibernate 3.0.5 with Oracle 10g:

In my database, I have 3 tables:

PALETTE
- ID (String-type GUID)
- NAME (String containing values such as 'Default user palette", "Custom user palette 1")

COLOR
- ID (String - 'human' defined values such as 'bgcolor', 'font1', 'font2' for developers to use)
- NAME (String containing values such as 'Background Color', 'Primary font color', 'Secondary font color')

PALETTE_COLORS
- PALETTE_ID (should be many-to-one to match IDs in the PALETTE table)
- COLOR_ID (should be many-to-one to match IDs in the COLOR table)
----note: each PALETTE_ID + COLOR_ID combination must be unique
- COLOR_CODE (String value representations of a 6 digit hexadecimal color code, i.e. 'FFFFFF', '000000', '000033')

I've already defined a Palette class and a Color class, but would like to have a colors field/getter/setter in my Palette class:

private Properties colorMap
public Properties getColorMap
public void setColorMap(Properties colorMap)

The most frequent access to this data will be simply to be able to retrieve a colorMap from any given palette so that the color_code can be quickly retrieved via a String representation of the color_id (not by retrieving a Color object and passing it in)... i.e.

Palette myPalette;
...
code to retrieve proper palette via hibernate
...
Properties myColorMap = myPalette.getColorMap();
String myBackgroundColor = myColorMap.getProperty("bgcolor");
String myPrimaryFontColor = myColorMap.getProperty("font1");
String mySecondaryFontColor = myColorMap.getProperty("font2");

or, better yet, via JSP tags, i.e.

<PAL:PAL color="bgcolor" />
(of course after some kind of initialization that allows this tag to already know the proper palette!)

In other cases (less frequently accesed) I will need to present to the user a form that they can use to update their palette. The form generation would be:

create hidden form element with PALETTE_ID value - name is arbitrary
iterate through each COLOR in the table and output the following as label, form element name, form element value {
COLOR.NAME
COLOR.ID or (PALETTE_COLORS.COLOR_ID)
PALETTE_COLORS.COLOR_CODE
}

Then, in turn, when these values are posted, the back-end data needs to be inserted/updated properly.

designing these objects in a way that makes sense for both is where I'm stuck. This is a new development (no legacy data) so I can pretty much structure this any way that makes the most sense. Any good suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 1:13 pm 
Regular
Regular

Joined: Thu Oct 27, 2005 8:06 am
Posts: 55
Location: München, Germany
I'd use

Code:
in Palette

set paletteColors lazy="false" inverse="true"

in Color

set paletteColors inverse="true"

in PaletteColors

many-to-one palette
many-to-one color lazy="false"


This is the standard way of mapping this kind of thing, and it will give you fast access in the Palette -> PaletteColor -> Color direction, which is what you mostly need.

If this is not what you need, please explain further.


Top
 Profile  
 
 Post subject: This would work but...
PostPosted: Thu Dec 08, 2005 2:09 pm 
Newbie

Joined: Thu Dec 08, 2005 12:19 pm
Posts: 6
Wouldn't this only give me access to a Set of PaletteColor objects from Palette? What I'm hoping for is a Map that contains color_id, color_code key/value pairs... but that's only part of the solution. I also want to be able to do this without causing complications in the rest of the design (see original desciption).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 09, 2005 2:20 am 
Regular
Regular

Joined: Thu Oct 27, 2005 8:06 am
Posts: 55
Location: München, Germany
Sorry, I didn't notice you need a map. Section 7.2.3 of the Hibernate reference describes how to handle maps. Somehow, I fail to see the complications this would have on the design.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 09, 2005 11:11 am 
Newbie

Joined: Thu Dec 08, 2005 12:19 pm
Posts: 6
What makes this tricky is that I want a map that uses Strings for keys, not Color objects, and preferrably (but not essential) Strings for the values as well. COLOR_ID, COLOR_CODE. If I am not doing it this way, then I would have to retrieve a Color object just to look up the COLOR_CODE (or PaletteColor) within the Map, when really, in this particular use case, the extra information that would be stored in the COLOR table is not needed (not shown because it's not important here). It's only needed for the administrative side of the application when managing the palettes, not just retrieving the color codes for purposes of setting style sheets.

Is there a way to do this within Hibernate (create String,String Maps from what I've described)? I've read about Maps in hibernate, but I just don't see how it fits with this, if it does at all. I know that with JDBC I could query just the single PALETTE_COLORS table to get the data that I want (after having identified the PALETTE_ID that I want to use).


Top
 Profile  
 
 Post subject: Solution found
PostPosted: Mon Dec 12, 2005 1:00 pm 
Newbie

Joined: Thu Dec 08, 2005 12:19 pm
Posts: 6
This mapping in my hibernate mapping XML for Palette does the job:

<map name="colorMap" table="PALETTE_COLORS">
<key column="PALETTE_ID"/>
<map-key column="COLOR_ID" type="string"/>
<element column="COLOR_CODE" type="string"/>
</map>

This makes myPalette.getColorMap() do what is preferred. Thanks anyway for the attempts to help. I hope this information helps someone else in the future.


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