Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] @ManyToMany with a discriminator in the @JoinTable

In general I would recommend you just create two tables.

However, you can do this using the Expression criteria API on the
ManyToManyMapping.

See,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/MappingSelectionCriteria

Your Expression will be a little more complex than this example because it
is a ManyToMany, but more or less the same, you will just need to use
getTable() in the Expression to access the join table.


Zarar Siddiqi wrote:
> 
> I have a situation where two entities can have a @ManyToMany
> relationship with another entity.  In the following case Auditor and
> Agent have a collection of Sections associated with them:
> 
> public class Agent {
>     @Id
>     @Column("ID")
>      private Long id;
> 
>     @ManyToMany
>     @JoinTable(
>             name="SECTION_MAPPING",
>             joinColumns={@JoinColumn(name="ID")},
>             inverseJoinColumns={@JoinColumn(name="SECTION_ID")}
>         )
>      private List<Section> sections;
> }
> 
> 
> public class Auditor {
>     @Id
>     @Column("ID")
>      private Long id;
> 
>     @ManyToMany
>     @JoinTable(
>             name="SECTION_MAPPING",
>             joinColumns={@JoinColumn(name="ID")},
>             inverseJoinColumns={@JoinColumn(name="SECTION_ID")}
>         )
>      private List<Section> sections;
> }
> 
> I want to model the SECTION_MAPPING table to be something like this:
> 
> SECTION_MAPPING ( ID, SECTION_ID, DISCRIMINATOR)
> 
> where DISCRIMINATOR has the value of either "auditor" or "agent".
> 
> Is there any way I can have a shared @JoinTable so I can avoid
> creating two tables like AGENT_SECTIONS and AUDITOR_SECTIONS?
> 
> I realize I could achieve this using GUIDs but I don't have that luxury.
> 
> Any help is appreciated.
> 
> Thanks,
> Zarar
> 
> 


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/%40ManyToMany-with-a-discriminator-in-the-%40JoinTable-tp23051504p23059726.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top