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.