-->
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: Is a mapping table always needed?
PostPosted: Thu Aug 13, 2009 4:21 am 
Newbie

Joined: Thu Aug 13, 2009 3:52 am
Posts: 3
Hi!

I'm quite new to using hibernate, so be gentle.

I'm setting up a rule engine, which handles lists that are defined by a set of rules. The engine should, given a list id, return a list of detailed rules for that list.
A list is only defined by its list id, it has no extra properties.
A rule has a set of properties, defining the rule.
One list can be defined by several rules.
The same rule can be used by several lists.

Is the relation then what is called a one-to-many relation? (Since one list can have several rules)
And a unidirectional one? (Since I only want to send in the list id, and retrieve the rules from that)

The way I wanted to map this was something like:

Table Lists:
List_id | Rule_id
listA | rule1
listA | rule2
listB | rule1
listC | rule3

Table Rules
rule_id | rule_info1 | rule_info2
rule1 | xxx | yyy
rule2 | aaa | dddd
rule3 | bbb | ccc

But it seems that I must have a mapping table in between. So the table Lists now becomes a new table List_Rule_Mapping
and a new table Lists is created with one column, with the list ids.
Is that correct?

Cheers,
E


Top
 Profile  
 
 Post subject: Re: Is a mapping table always needed?
PostPosted: Thu Aug 13, 2009 8:59 am 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
In this kind of scenarios, you could do two approaches:
a) To define Lists' id like composite id {list_id, rule_id}: this only would have sense if there is no other attributes in Lists table, but id. This approach is not usual.
b) To keep the actual design and map the relation as ManyToMany, so it MUST to generate an intermediate table.

_________________
Born to lose... live to win!


Top
 Profile  
 
 Post subject: Re: Is a mapping table always needed?
PostPosted: Thu Aug 13, 2009 10:25 am 
Newbie

Joined: Thu Aug 13, 2009 3:52 am
Posts: 3
Ok.
I'm working on the second approach right now, but the first one feels tempting.
There are no other attributes to the Lists table but the id. With your first approach, is it still possible to send questions like give me all rules where list_id = 'listA'?
In some way I want my query to return a collection of ListRule objects.


Top
 Profile  
 
 Post subject: Re: Is a mapping table always needed?
PostPosted: Thu Aug 13, 2009 11:41 am 
Newbie

Joined: Tue Feb 24, 2009 8:39 pm
Posts: 19
It seems you have a One(list)ToMany(tasks) relationship, therefore a bridge table is not needed.
A bridge table is always needed in case of Many(list)ToMany(tasks).

You must read java persistance with hibernate.

Class Lists
Code:
@Entity
public class Lists {
  int list_id;
  Set<Tasks> tasks;
 
@Id
public int getList_id() {
   return list_id;
}
@OneToMany
@JoinColumn
public Set<Tasks> getTasks() {
   return tasks;
}
public void setList_id(int listId) {
   list_id = listId;
}
public void setTasks(Set<Tasks> tasks) {
   this.tasks = tasks;
}
}


Class Tasks
Code:
@Entity
public class Tasks {
  int rule_id;
  String ruleInfo1;
  String ruleInfo2;

@Id
public int getRule_id() {
   return rule_id;
}
public String getRuleInfo1() {
   return ruleInfo1;
}
public String getRuleInfo2() {
   return ruleInfo2;
}
public void setRule_id(int ruleId) {
   rule_id = ruleId;
}
public void setRuleInfo1(String ruleInfo1) {
   this.ruleInfo1 = ruleInfo1;
}
public void setRuleInfo2(String ruleInfo2) {
   this.ruleInfo2 = ruleInfo2;
}
}


best regards.


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.