users@glassfish.java.net

Re: More columns in 'ManyToMany' JoinTable

From: <glassfish_at_javadesktop.org>
Date: Mon, 12 Feb 2007 07:35:26 PST

Hey,

regarding ManyToMany relationships, I have two questions


1)
What is the best practive of managing additional columns in the JoinTable of ManyToMany relationships entities.

For instance, assuming I have two entities such as:
public class User {
  //some fields such as
  int id
  String firstName;
  String lastName;
}

Group {
  @ManyToMany(fetch=FetchType.LAZY,targetEntity=entities.User.class)
  @JoinTable(
        name="USERS_TO_GROUP",
        joinColumns=_at_JoinColumn(name="GROUP_ID"),
        inverseJoinColumns=_at_JoinColumn(name="USER_ID")
  )
  public Collection<Account> getAccounts();
}

I'd like in the JoinTable to have some extra fields such as 'assignmentDate/assignedBy' fields, and manage them easily...


I assume I can create another entity such as UserGroup for that, but maintanance grows that way since I need extra columsn in JoinTables in many places...





2) If one of the 'User' or 'Group' objects being removed, the relationship remains. is there any way to automatically remove the relationship? I do not want to remove the connected -objects-, only the relationships when one of the sides being removed.


As always. thanks,

Asaf.
[Message sent by forum member 'trouby' (trouby)]

http://forums.java.net/jive/thread.jspa?messageID=203418