3

I have a table what represent a many to many relationship.

+----------+           +------------+           +----------+
|   User   |           | UserGroups |           |  Group   |
+----------+           +------------+           +----------+
| id   (PK)|---(1,N)---| user_id    |---(N,1)---| id   (PK)|
| year (PK)|           | year       |           | year (PK)|
| name     |           | group_id   |           | name     |
+----------+           +------------+           +----------+

In Java, I have only the entity User and Group, and I don't like to have UserGroups in Java model because It didn't provide more information that the relationship. My class User is like that:

@Entity
@Table(name="User")
public class User {
  @EmbeddedId
  private UserPK id;
  @ManyToMany
  @JoinTable(name = "UserGroups",
             joinColumns = { @JoinColumn(name = "user_id", referencedColumnName="id"),
                             @JoinColumn(name = "year", referencedColumnName="year") },
             inverseJoinColumns = { @JoinColumn(name = "group_id", referencedColumnName = "id"),
                                    @JoinColumn(name = "year", referencedColumnName = "year") })
  private List<Group> groups;
}

When I try to run the application I get this exception:

org.hibernate.MappingException:Repeated column in mapping for collection: com.testapp.model.entities.User.groups column: year

I don't want to have UserGroups in my Java model. I don't want to create another year column in UserGroups table. How I can do this relation in Java?

earnaz
  • 335
  • 1
  • 4
  • 19
  • This is supposed to be possible, see [Many-to-many with shared composite key attribute](http://docs.jboss.org/hibernate/orm/3.5/reference/en/html/example-mappings.html#example-mappings-composite-key-manytomany). – Vlastimil Ovčáčík Sep 23 '15 at 12:13

1 Answers1

0

I am not at expert in this topic, at all. But I do know the following:

The join table can relate/connect User and Group entities through their IDs only because they are unique. At the moment you are adding the field year int he equation, you are ignoring that requirement.

Conclusion: You have to remove year from the join table.

Option: Making unique as couple "user_id & year" for User table and "group_id & year" for Group table might work. I doubt that though.

kazbeel
  • 1,378
  • 19
  • 40
  • Sorry but It's not the solution. Year is part of the primary key in User and Group. In my model is not the same User(id=1, year=2013) than User(id=1, year=2014), so I couldn't remove the year column. But thanks for the answer. – earnaz May 26 '14 at 08:58
  • Hmm! Very interesting DB design I have to say... Anyhow, have you tried to generate a custom PK by using/mixing `user_id` and `year`? Hibernate offers such possibility. – kazbeel May 26 '14 at 11:29
  • Take a look http://stackoverflow.com/questions/13936340/to-create-hibernate-composite-key-using-annotations and http://stackoverflow.com/questions/1212058/how-to-make-a-composite-primary-key-java-persistence-annotation. – kazbeel May 26 '14 at 11:36
  • If i don't use JoinTable annotation and I use EmbeddedId, Hibernate try to search "user_year" in UserGroups, so I need to specify what column's name is "year" with JoinTable annotation, So I have again the origin problem for use JoinTable annotation. – earnaz May 26 '14 at 11:55
  • Give it a try: http://stackoverflow.com/questions/6405746/mapping-manytomany-with-composite-primary-key-and-annotation – kazbeel May 26 '14 at 12:45
  • That is not my case. I don't have the annotations in getters and my many-to-many table have two keys per table, not one per table. – earnaz May 26 '14 at 13:22