I created a class called UserGroup. UserGroup is a self-join. The root has its own sub-UserGroups and sub-UserGroups have its own children.
Code:
@Entity
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"groupName"})})
public class UserGroup extends BusinessObject {
private String groupName;
private UserGroup parent;
private List<UserGroup> children;
private List<User> users;
@Column(length=50, nullable=false)
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
@ManyToOne
public UserGroup getParent() {
return parent;
}
public void setParent(UserGroup parent) {
this.parent = parent;
}
@OneToMany(mappedBy="parent")
@Cascade ({CascadeType.SAVE_UPDATE})
@OrderBy("groupName")
public List<UserGroup> getChildren() {
if(null == this.children){
this.children = new LinkedList<UserGroup>();
}
return children;
}
public void setChildren(List<UserGroup> children) {
this.children = children;
}
public void addChild(UserGroup userGroup){
this.getChildren().add(userGroup);
userGroup.setParent(this);
}
@OneToMany(mappedBy="userGroup")
@Cascade ({CascadeType.SAVE_UPDATE})
public List<User> getUsers() {
if(null == this.users){
return new LinkedList<User>();
}
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public void addUser(User user){
this.getUsers().add(user);
user.setUserGroup(this);
}
@Transient
public boolean isRoot(){
return null == this.getParent();
}
public void initialize(){
if(getChildren() != null){
Hibernate.initialize(getChildren());
}
}
}
Also, I created a controller:
Code:
public class UserGroupListController extends GenericSingleViewController {
private SecurityService securityService;
public SecurityService getSecurityService() {
return securityService;
}
public void setSecurityService(SecurityService securityService) {
this.securityService = securityService;
}
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView resultPage = new ModelAndView(getViewPage());
UserGroup userGroup = securityService.getUserGroup(new Long(1));
logger.debug("is Open --> ", Hibernate.isInitialized(userGroup.getChildren()));
userGroup.initialize();
resultPage.addObject("root", userGroup);
return resultPage;
}
}
I create a jsp file:
Code:
<%@ include file="/WEB-INF/tags/tags.inc"%>
<layout:mainLayout pageTitle="">
<layout:leftContent title="User Group List" collapsible="false">
<div id="tree" style="margin-top: 10px;"></div>
</layout:leftContent>
<layout:mainContent>
<c:if test="${!empty root}">
<c:forEach var="child" items="${root.children}">
<p>${child.groupName}</p>
</c:forEach>
</c:if>
</layout:mainContent>
</layout:mainLayout>
Finally, it throws error:Quote:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: collection is not associated with any session
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:472)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:415)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
org.hibernate.HibernateException: collection is not associated with any session
org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:471)
org.hibernate.Hibernate.initialize(Hibernate.java:332)
com.rj.opencrm.model.security.UserGroup.initialize(UserGroup.java:87)
com.rj.opencrm.web.controller.security.UserGroupListController.handleRequestInternal(UserGroupListController.java:36)
org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:839)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:774)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:460)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:415)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
Can any one help me solve this problem?