macemers
macemers

Reputation: 2222

Add extra property to intermediary table when mapping many-to-many association with Hibernate

In database, I have following three tables, where User and Profile have many-to-many relationship and joined with User_Profile as follow:

User                User_Profile            Profile
----                ------------            ------- 
user_id             user_id                 profile_id
user_name           profile_id              profile_description
user_time

Class UserHbm and ProfileHbm are used:

@Entity
@Table(name = "User")
public class UserHbm {

    @Id
    @GeneratedValue(generator = "id-generator")
    @GenericGenerator(name = "id-generator",
            strategy = "com.xx.xxx.XXXSequenceGenerator", 
            parameters = { @Parameter(name = "sequenceName", value = "User") })
    @Column(name = "user_id")
    private long user_id;   

    @Column
    private String user_name; 

    @Column
    private Date user_time; 

    @ManyToMany(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
    @JoinTable(name="User_Profile", 
            joinColumns = { @JoinColumn(name = "identityProfileID") },
            inverseJoinColumns={@JoinColumn(name="profile_id")})
    private Set<ProfileHbm> profiles = new HashSet<ProfileHbm>();

    //irrelevant other codes

}

@Entity
@Table(name = "Profile")
public class ProfileHbm {

    @Id
    @GeneratedValue(generator = "id-generator")
    @GenericGenerator(name = "id-generator",
            strategy = "com.xx.xxx.XXXSequenceGenerator", 
            parameters = { @Parameter(name = "sequenceName", value = "Profile") })
    @Column(name = "profile_id")
    private long profile_id;    

    @Column 
    private String profile_description;   

    //irrelevant other codes

}

So far, everything is fine until a new requirement comes up: put user_time into User_Profile so that the schema looks like:

User                User_Profile            Profile
----                ------------            ------- 
user_id             user_id                 profile_id
user_name           profile_id              profile_description
user_time           user_time

Could anyone could show me how to achieve this?

Do I have to create another intermediary HBM to do this?

Upvotes: 0

Views: 1577

Answers (2)

Apokralipsa
Apokralipsa

Reputation: 2724

If you just need to add a single column to the mapping table you can just change your UserHbm class. Instead of:

@ManyToMany(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
@JoinTable(name="User_Profile", 
        joinColumns = { @JoinColumn(name = "identityProfileID") },
        inverseJoinColumns={@JoinColumn(name="profile_id")})
private Set<ProfileHbm> profiles = new HashSet<ProfileHbm>();

Use:

@ElementCollection
@JoinTable(name="User_profile")
@MapKeyJoinColumn(name="profile_id")
@Temporal(TemporalType.TIMESTAMP)
@Column(name="user_time")
private Map<Profile, Date> profiles = new HashMap<>();

You can even change the value of the map to an @Embeddable class, when you need to put more columns in the table.

Upvotes: 0

Olivier Meurice
Olivier Meurice

Reputation: 562

A solution is to create a new entity for UserProfile join table.
Set a OneToMany relationship from User entity to new UserProfile entity and a OneToMany from Profile to User_Profile.
You will have to create an additional class, let say UserProfilePk, for the User_Profile entity for the composite index composed of user_id and profile_id.

@Embeddable
public class UserProfilePk {
  @ManyToOne
  private User

  @ManyToOne
  private Profile
}

Then you have to use that class for the User_Profile index:

@Entity
public class UserProfile {
  @EmbeddedId
  private UserProfilePk pk;

  @Column(name = "user_time")
  private Date userTime;

}

Your class User:

@Entity
public class User {
  @Id
  private Long id;

  private String name;

  private Date userTime;

  @OneToMany(mappedBy = "pk.user", cascade=CascadeType.ALL)
  private Set<UserProfile> userProfiles;
}

And the class Profile:

@Entity
public class Profile {
  @Id
  private Long id;

  private String description;

  @OneToMany(mappedBy = "pk.profile")
  private Set<UserProfile> userProfiles;
}

And here below the code to save a User and an associated Profile:

Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.getCurrentSession();

User user = new User();
user.setId(1l);
user.setName("Scott");
user.setUserTime(new Date());
Profile profile = new Profile();
profile.setId(1l);
profile.setDescription("some user");

Transaction tx = session.beginTransaction();     
session.save(profile);

UserProfilePk pk = new UserProfilePk();
pk.setProfile(profile);
pk.setUser(user);

UserProfile userProfile = new UserProfile();
userProfile.setPk(pk);
userProfile.setUserTime(new Date());

Set<UserProfile> ups = new HashSet<>();
ups.add(userProfile);    
user.setUserProfiles(ups);

session.save(user);
tx.commit();

Upvotes: 1

Related Questions