Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ternary Association

 
Luigi Rubino
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not clear how to configure a ternary association with Hibernate.
Some help?
Thanks,
Luigi
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Luigi Rubino:
It's not clear how to configure a ternary association with Hibernate.
Some help?
Thanks,
Luigi


Collection Mapping

Ternary Associations
There are two possible approaches to mapping a ternary association. One approach is to use composite elements (discussed below). Another is to use a Map with an association as its index:

<map name="contracts" lazy="true">
<key column="employer_id"/>
<index-many-to-many column="employee_id" class="Employee"/>
<one-to-many column="contract_id" class="Contract"/>
</map>
<map name="connections" lazy="true">
<key column="node1_id"/>
<index-many-to-many column="node2_id" class="Node"/>
<many-to-many column="connection_id" class="Connection"/>
</map>
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somkait

Could you please the URL where you found the above?
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pradeep Bhat:
Somkait

Could you please the URL where you found the above?



http://www.hibernate.org/hib_docs/reference/en/html/collections.html#collections-ternary
 
Gavin King
author
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A further way is to just model the association as an entity class :-)
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the meaning of ternary association? (I couldn't figure it out from the reference :-( ).
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ali Pope:
What is the meaning of ternary association? (I couldn't figure it out from the reference :-( ).



Types of Associations
1. By type of verbs (Rumbaugh p. 156):

Associations often correspond to verbs. These include:

Physical Location (Next to, part of, contained in)
Directed actions (drives)
Communication (talks to)
ownership (has, part of)
Satisfaction of some condition (works for, married to, manages)
2. over one same object (i.e. Dancer IS NEXT TO Dancer) (Shlaer & Mellor p. 48)

3. Multiple relationships between two same objects. i.e., "Part IS OBTAINABLE FROM Supplier" "Part IS ON ORDER FROM Supplier"

4. The same object may participate in different relations that involve different objects. i.e., "Diskette WAS FORMATTED ON Disk Drive" "Diskette IS OWNED BY Person" "Diskette CONTAINS files"

5. Associations may also be: binary, ternary, or higher order. Binary associations involve two different classes. This is the most common. Higher order associations are more complicated to draw, implement, and think about than binary associations.
[b]Ternary associations involve 3 objects:

It is an atomic unit
The division of a ternary association into binary associations may loose information
The associations are represented by a diamond + the name of the association
Name of the association is optional




 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And more about Ternary Associations

Definition:
Ternary associations are those that involve three first-rank classes. First-rank classes A, B and C are associated together in a relationship.

Scenario:
We have first-rank classes Foo, Bar and Snafu which are all related together. We choose to store their relationship in Foo using a composite class BarSnafu:

Bar BarSnafu.getBar() // Retrieve Bar from BarSnafu
Snafu BarSnafu.getSnafu() // Retrieve Snafu from BarSnafu
Set Foo.getBarSnafus() // Retrieve set of BarSnafus from Foo


Hibernate Mapping:
In Hibernate, this could be mapped as follows:




Table Schema:

1. Foo
id

2. Bar
id

3. Snafu
id

4. Foo_Bar_Snafu
foo_id
bar_id
snafu_id


So here we've got three first-rank classes. They are related together in table foo_bar_snafu. The result is stored as a set of BarSnafu instances on each Foo. Each BarSnafu instance references one Bar and one Snafu.

Simple huh?

NB: Using composite elements we can go beyond this and support 4 and more elements in a relationship.

Bidirectionality:
Bidirectionality has no meaning for a ternary relationship.
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for explanations/hints. Still I don't see how this is different from a normal relation :-(. ABC is related to A, B, and respectively C. Hmmm... seems in dunno get it [snif/].
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ali Pope:
Thanks for explanations/hints. Still I don't see how this is different from a normal relation :-(. ABC is related to A, B, and respectively C. Hmmm... seems in dunno get it [snif/].


It's in fact a combination of multiple
normal relations where the combination
itself has to be represented as a relation.

Cheers,

Gian Franco Casula
[ September 01, 2004: Message edited by: Gian Franco Casula ]
 
Luigi Rubino
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all.
I'm new to Hibernate, and I like it very much.
I think that the solution using an entity class is the most intuitive, and permits to manage all the attributes of the entity class.

Special thanks to Gavin for this great persistence framework.

Best regards,
Luigi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic