-->
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.  [ 4 posts ] 
Author Message
 Post subject: Design question: Mapping multiple Set of similar objects
PostPosted: Thu Jan 12, 2006 9:16 am 
Newbie

Joined: Fri Dec 30, 2005 5:25 am
Posts: 13
Hibernate version: 3.0.6
Name and version of the database you are using: MySQL

I have a design best practices questions.

Let's say I have an class that has 3 java.util.Set which I want to map.

Code:
package domain.tool;

import java.util.Set;

public class Tools {
   private Set<BusinessIntelligenceTool> businessIntelligence;
   private Set<MonitoringTool> monitoringTools;
   private Set<BuilderTool> buildTools;
}


All the 3 classes (BusinessIntelligenceTool, MonitoringTool and BuilderToon) extend the same parent class (AbstractTool), and therefore have the same fields (name). Typically, the one-to-many relationship that the collections create would require me to create 3 additonnal tables to store the relationships. I'd like to avoid that.

I'd like to know if it's possible to store all the entries in 1 table. This would for example be the mapping for one of the 3 classes in one of my Sets.
They'd all write to the table "TOOLS" which would be common for the all 3 classes...

Code:
<class name="domain.tool.BusinessIntelligenceTool" table="TOOLS">
    <id name="id" column="ID" type="int">
       <generator class="native"/>
    </id>
    <property name="name" type="string">
       <column name="NAME" not-null="true" index="TOOL_NAME"/>
    </property>

    <property name="fk" type="int">
       <column name="BI_TOOL_ID" />
    </property>
    <many-to-one name="project" class="domain.tool.Tools">
       <column name="TOOLS_ID" not-null="true"/>
    </many-to-one>
</class>


Is that a good design? Anything else to suggest?
Here are my questions.

- How can I map the 3 collections the better. Do I need to create 3 different tables, or is it possible to merge them into one...
- Wouldn't a kind of many-to-many be more appropriate?

Thanks a lot for your input!

Cheers,

Nicolas


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 9:27 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
You may consider to use inheritance mapping as explained in the reference. In this case you have the choice to only create one class. The types can be distinguished by a discriminator.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 5:39 pm 
Newbie

Joined: Fri Dec 30, 2005 5:25 am
Posts: 13
Well, I'm afraid my question was not really clear...

The problem is not the mapping of the inheritance (with a discriminator), it's the way that I can map my Collections (the 3 java.util.Set's) which all require a separate table to represent the one-to-many relationship.

(Actually, in my real example, I have 9 sets). This make a lot of unnecessary tables, which I'd like to avoid if it's possible. I'd like to group the information in a main table, especially considering all the objects have a similar type of info (since they inherit from the same parent class).

I have looked in the sample in the Hibernate in Action book and I could not find a good example of that. I realise this is more a general design question than a pure Hibernate API question.

Hope this clears up the confusion.

Thanks!!

Nicolas


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 5:49 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Still look at the inheritance. Table per hierarchy. There is a possibility to have one table with all information in it.

Another idea use composition.
Tool
class Special Tool(
private Tool too;
private Specialtooattribute x;
...

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


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