1

My app has 2 java pojo classes linked via ManyToMany relationship:

@Entity
@Table(name = "user")
public class User implements Serializable {

@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "user_match", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "match_id") })
    private Set<Season> followingSeason;

}

Season class

@Entity
@Table(name = "cricket_match")
public class Season implements Serializable{

@ManyToMany(mappedBy = "followingSeason", fetch = FetchType.EAGER)
    private Set<User> user;

}

On the UI I have a season button clicking on it sets the season with the user. There is a pre-existing user entry as well as the season entry in the db table. Basically I am updating the user object with an additional season object info.

if ("true".equalsIgnoreCase(followSeason)) {
Season season = new Season(); // I cannot instantiate a new season object here as it clears all the pre-existing entries of the season row of the entered season id. Do I load the season object first & then set it with the user object?
season.setSeasonId(SeasonId);
Set<Season> seasonSet = new HashSet<Season>();
seasonSet.add(season);
loggedInUser.setFollowingSeason(seasonSet); //loggedInUser is a User object
this.myService.followSeason(loggedInUser);

I cannot instantiate a new season object here as it clears all the pre-existing entries of the season row of the entered season id. Do I load the season object first from the db & then set it with the user object? I am not sure would this be the correct approach.


public void followSeason(User loggedInUser){
        sessionFactory.getCurrentSession().update(loggedInUser);
    }

From the logs

Hibernate: delete from user_season where user_id=?
2015-12-30 18:16:03.0999 DEBUG http-bio-8080-exec-6 org.hibernate.persister.collection.AbstractCollectionPersister – Done deleting collection
2015-12-30 18:16:04.0005 DEBUG http-bio-8080-exec-6 org.hibernate.persister.collection.AbstractCollectionPersister – Inserting collection: [.User.followingSeason#1]
2015-12-30 18:16:04.0011 DEBUG http-bio-8080-exec-6 org.hibernate.SQL – insert into user_season (user_id, season_id) values (?, ?)
Hibernate: insert into user_season (user_id, season_id) values (?, ?)
underdog
  • 4,447
  • 9
  • 44
  • 89

1 Answers1

1

Yes, you have to load the owner of the association from the db. You can take a look at this answer which provides some mechanisms about how to optimize many-to-many associations.

However, I see that the User is actually the owner of the association, not Season. And it seems that your mappings are not correct: instead of mappedBy = "followingMatch" you probably mean mappedBy = "followingSeason".

Third point, you should use plural for collections (followingSeasons, users) to make the code more readable.

Community
  • 1
  • 1
Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110
  • well in my knowledge in a ManyToMany relationship any entity can be the owner. Correct me if I am wrong. – underdog Dec 30 '15 at 15:55
  • You're wrong. :) Please take a look at the [documentation](https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch07.html#collections-bidirectional): _"If the association is bidirectional, one side has to be the owner and one side has to be the inverse end."_ If this wasn't the case, both sides would be synchronized with the database if both are loaded in the same transaction, meaning that Hibernate would try to insert the row in the association table twice. – Dragan Bozanovic Dec 30 '15 at 15:59
  • thnx for the reply Dragan just one more question how do you determine which side is the owning side of the relationship? – underdog Dec 30 '15 at 16:07
  • The one that is defined with `mappedBy` is the inverse side, the other one is the owner. – Dragan Bozanovic Dec 30 '15 at 16:09
  • Dragan there is another issue in the continuation of the same question has arised. Would you help? I've updated the code, the update method of hibernate is deleting the previous values from the many to many bridge table. save is giving already existing object error. – underdog Dec 30 '15 at 18:40
  • It seems that you are always setting a new `Set` to the `loggedInUser`. Add the new `Season` to the existing set: `loggedInUser.getFollowingSeason().add(season)`. – Dragan Bozanovic Dec 30 '15 at 19:08