I'm noticing a very similar issue after I created a reversible OneToOne relationship.
A brief synopsys of my mappings:
Code:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type", discriminatorType=DiscriminatorType.STRING)
public abstract class Filter {
private int id = 0;
protected Cluster cluster = null;
private String name = "";
protected Filter(){
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}
protected void setId(int id) {
this.id = id;
}
@Column(unique=true, updatable=false)
public String getName() {
return name;
}
protected void setName(String name) {
this.name = name;
}
@OneToOne(cascade={CascadeType.ALL})
@JoinColumn(updatable = false, nullable = true)
public Cluster getCluster() {
return cluster;
}
protected void setCluster(Cluster cluster) {
this.cluster = cluster;
}
}
The ManyToMany in question however, is found in a class that extends the above one:
Code:
@Entity
@DiscriminatorValue(value="accessFilter")
public class AccessFilter extends Filter {
private List<UserRole> userRoles = new ArrayList<UserRole>();
protected AccessFilter(){
}
@ManyToMany(targetEntity=UserRole.class, fetch=FetchType.EAGER)
@JoinTable(name="UserRole_Filter", joinColumns=@JoinColumn(name="filter_id"), inverseJoinColumns=@JoinColumn(name="userRole_id"))
private List<UserRole> getUserRoles() {
return userRoles;
}
private void setUserRoles(List<UserRole> userRole) {
this.userRoles = userRole;
}
}
And the reversed OneToOne from Filter:
Code:
@Entity
public class Cluster{
private Filter filter = null;
private int id = 0;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}
protected void setId(int id) {
this.id = id;
}
@OneToOne(mappedBy="cluster")
public Filter getFilter(){
return filter;
}
private void setFilter(Filter filter){
this.filter = filter;
}
}
If I comment out the reverse side of the OneToOne relationship (the reference in class Cluster) between Filter and Cluster, everything in AccessFilter is fine. However, with the reversed side of the OneToOne implemented, I get duplicate values in userRole set of AccessFilter (single values repeated 10's of times).
These duplicate values can not be found in the database, it appears that hibernate is selecting these rows multiple times.
My hibernate versions:
Hibernate Annotations 3.3.0.GA
Hibernate 3.2.5
Hibernate EntityManager 3.3.1.GA
running in Jboss 4.0.5.GA