Hibernate version: Hibernate for EJB3, on JBoss, with SQL Server.
I'm facing a problem similar to that in the JPWH book (page 588-589): our "User" entity retrieves several collections in a many-to-many relationship, and as a result we're getting multiple left outer joins, i.e. a Cartesian product.
The book suggests using a subselect strategy, but I've not been able to make that work. For instance, where the original code is like this:
Code:
@ManyToMany(fetch=FetchType.EAGER, cascade=CascadeType.MERGE)
@JoinTable(name="fe_user_to_fe_internal_group",
joinColumns = @JoinColumn(name = "fe_user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "fe_internal_group_id", referencedColumnName = "id"))
public Set<InternalGroup> getInternalGroups() {
return internalGroups;
}
@ManyToMany(fetch=FetchType.EAGER, cascade=CascadeType.MERGE)
@JoinTable(name="fe_user_to_fe_webos_menu_item",
joinColumns=@JoinColumn(name="fe_user_id"),
inverseJoinColumns=@JoinColumn(name="fe_webos_menu_item_id"))
public Set<WebosMenuItem> getMenuItems() {
return this.menuItems;
}
...I've tried to mitigate the problem by inserting a Hibernate annotation:
Code:
@ManyToMany(fetch=FetchType.EAGER, cascade=CascadeType.MERGE)
@org.hibernate.annotations.Fetch(value = org.hibernate.annotations.FetchMode.SUBSELECT)
@JoinTable(name="fe_user_to_fe_internal_group",
joinColumns = @JoinColumn(name = "fe_user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "fe_internal_group_id", referencedColumnName = "id"))
public Set<InternalGroup> getInternalGroups() {
return internalGroups;
}
@ManyToMany(fetch=FetchType.EAGER, cascade=CascadeType.MERGE)
@org.hibernate.annotations.Fetch(value = org.hibernate.annotations.FetchMode.SUBSELECT)
@JoinTable(name="fe_user_to_fe_webos_menu_item",
joinColumns=@JoinColumn(name="fe_user_id"),
inverseJoinColumns=@JoinColumn(name="fe_webos_menu_item_id"))
public Set<WebosMenuItem> getMenuItems() {
return this.menuItems;
}
This doesn't seem to be helping, though. What am I doing wrong?