persistence@glassfish.java.net

Problem using ManyToMany relationship

From: Romanowski, Tim <tim.romanowski_at_lmco.com>
Date: Mon, 26 Mar 2007 16:55:31 -0400

Tricky problem I thought I solved, and I hope someone could explain to me
both 1) why this kind of works, and 2) why it doesn't completely work.

 

Situation: I have a ManyToMany relationship between two entities, Team and
User. A team has many users and a user has many teams. I have a join table
called team_users which contains two fields: teamid and username, which map
to the corresponding primary keys of the Team and User tables.

 

Problem: Creating a 'standard' ManyToMany relationship where I pick an
arbitrary owning entity and annotate a member variable (rather than its get
method) does not work. The only way for me to compile and deploy my webapp
is to annotate these two entities as you see below.

 

Questions:

1) In the Team entity, why doest the @JoinTable annotation on the
getUserCollection method work, but not work if I instead place that same
annotation on the userCollection property?

2) In the User entity, why must I use the @AttributeOverride
annotation on the teamCollection property?

3) Why does NetBeans report "Inconsistent access type in entity class
because a field in this class or its super class chain is already annotated
as @Id or @EmbeddedId." This error is shown as a red squiggly line under
getUserCollection in the Team entity. BUT.I can still compile and deploy,
and run my app.

 

Would someone please explain to me some of the details with what is going on
here? Perhaps my entire approach is backward?

I am compiling with JDK 1.5.0_11-b3 and running on Glassfish V2 Build 40.
This problem goes back to builds in the mid 20s (when I started using these
entites).

 

 

Team Entity:

<snip/>

    @Id @GeneratedValue(generator="teamid_gen")

    @Column(name = "TEAMID", nullable = false)

    private BigDecimal teamid;

            

<snip> now here's the reference to my collection of User entities. </snip>


    @ManyToMany

    private java.util.Collection <com.my.persistence.entity.Users>
userCollection;

 

 

<snip> and the only way for my code to compile and deploy (despite the NB
error), specifying the getter like so. </snip>

   @ManyToMany

   @JoinTable(name = "team_users",

        joinColumns = {_at_JoinColumn(name = "teamid", referencedColumnName =
"teamid")},

        inverseJoinColumns = {_at_JoinColumn(name = "username",
referencedColumnName = "username")})

    public java.util.Collection<com.my.persistence.entity.Users>
getUserCollection() {

        return userCollection;

    }

 

    public void
setUserCollection(java.util.Collection<com.my.persistence.entity.Users>
userCollection) {

        this.userCollection = userCollection;

    }

 

 

User entity:

            <snip/>

    @Id

    @Column(name = "USERNAME", nullable = false)

    private String username;

 

    @ManyToMany(mappedBy="userCollection")

    @AttributeOverride(name="teamCollection", column=_at_Column(name="teamid"))

    private java.util.Collection <com.my.persistence.entity.Team>
teamCollection;

 

 

<snip> Nothing special is done to the getters and setter for this
(arbitrarily selected) NON-owning side. </snip>

    public java.util.Collection <com.my.persistence.entity.Team>
getTeamCollection() {

        return this.teamCollection;

    }

 

    public void setTeamCollection(java.util.Collection
<com.my.persistence.entity.Team> teamCollection) {

        this.teamCollection = teamCollection;

    }

 

 

Tim