-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Mapping many-to-many association table with extra column(s)
PostPosted: Fri Feb 25, 2011 11:34 pm 
Newbie

Joined: Fri Jul 11, 2008 5:35 am
Posts: 4
Hello
I'm rather new to hibernate
I've encountered the following problem
DB contains 3 tables:
User and Service entities have many-to-many relationship and are joined with the SERVICE_USER table as follows:

USERS - SERVICE_USER - SERVICES

SERVICE_USER table contains additional BLOCKED column.

What is the best way to perform such a mapping?
These are my Entity classes

@Entity
@Table(name = "USERS")
public class User implements java.io.Serializable {

private String userid;
private String email;

@Id
@Column(name = "USERID", unique = true, nullable = false,)
public String getUserid() {
return this.userid;
}

.... some get/set methods
}

@Entity
@Table(name = "SERVICES")
public class CmsService implements java.io.Serializable {
private String serviceCode;

@Id
@Column(name = "SERVICE_CODE", unique = true, nullable = false, length = 100)
public String getServiceCode() {
return this.serviceCode;
}
.... some additional fields and get/set methods
}

I followed this example http://sieze.wordpress.com/2009/09/04/m ... using-jpa/
And created additional Classes

@Embeddable
public class UserServiceId implements Serializable{
private User user;
private CmsService service;

@ManyToOne
public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

@ManyToOne
public CmsService getService() {
return service;
}

public void setService(CmsService service) {
this.service = service;
}

public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;

UserServiceId that = (UserServiceId) o;

if (user != null ? !user.equals(that.user) : that.user != null)
return false;
if (service != null ? !service.equals(that.service) : that.service != null)
return false;

return true;
}

public int hashCode() {
int result;
result = (user != null ? user.hashCode() : 0);
result = 31 * result + (service != null ? service.hashCode() : 0);
return result;
}
}

@Embeddable
public class UserServiceId implements Serializable{
private User user;
private CmsService service;

@ManyToOne
public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

@ManyToOne
public CmsService getService() {
return service;
}

public void setService(CmsService service) {
this.service = service;
}

public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;

UserServiceId that = (UserServiceId) o;

if (user != null ? !user.equals(that.user) : that.user != null)
return false;
if (service != null ? !service.equals(that.service) : that.service != null)
return false;

return true;
}

public int hashCode() {
int result;
result = (user != null ? user.hashCode() : 0);
result = 31 * result + (service != null ? service.hashCode() : 0);
return result;
}
}

I also added such code to both User and CmsService classes
private List<UserService> userServices = new LinkedList<UserService>();
@OneToMany(fetch = FetchType.LAZY, mappedBy = "id.user", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
public List<UserService> getUserServices() {
return userServices;
}
and

@OneToMany(fetch = FetchType.LAZY, mappedBy = "id.service", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
public List<UserService> getUserServices() {
return this.userServices;
}

Here is some test code:

User user = new User();
user.setEmail("e2");
user.setUserid("ui2");
user.setPassword("p2");

CmsService service= new CmsService("cd2","name2");

List<UserService> userServiceList = new ArrayList<UserService>();

UserService userService = new UserService();
userService.setService(service);
userService.setUser(user);
userService.setBlocked(true);
service.getUserServices().add(userService);

userDAO.save(user);

The problem is that hibernate persists User object and UserService one. No success with the CmsService object

I tried to use EAGER fetch - no progress

Is it possible to achieve the behaviour I'm expecting with the mapping provided above?

Maybe there is some more elegant way of mapping many to many join table with additional column?

Thanks in advance.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.