This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Ternary Association Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Ternary Association" Watch "Ternary Association" New topic
Author

Ternary Association

Luigi Rubino
Greenhorn

Joined: Feb 26, 2004
Posts: 11
It's not clear how to configure a ternary association with Hibernate.
Some help?
Thanks,
Luigi


Luigi Rubino<br /> <br />SCJP (1.5, 1.4, 1.2), SCBCD (1.3) , SCWCD (1.4, 1.3), SCEA (Part I)<br />MCP
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
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>


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Somkait

Could you please the URL where you found the above?


Groovy
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
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

Joined: Aug 31, 2004
Posts: 76
A further way is to just model the association as an entity class :-)


Co-Author of <a href="http://www.amazon.com/exec/obidos/ASIN/193239415X/ref=jranch-20" target="_blank" rel="nofollow">Hibernate in Action</a>
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
What is the meaning of ternary association? (I couldn't figure it out from the reference :-( ).


blog - InfoQ.com
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
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

Joined: Jul 04, 2003
Posts: 1312
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

Joined: Jul 12, 2004
Posts: 995
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

Joined: Dec 16, 2003
Posts: 977
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 ]

"Eppur si muove!"
Luigi Rubino
Greenhorn

Joined: Feb 26, 2004
Posts: 11
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
 
GeeCON Prague 2014
 
subject: Ternary Association