Reputation: 678
I am having some problems getting a larger application with many table relationships working with Struts2 and Hibernate, so I decided to create a much smaller-scale example to figure it out and I cannot get that working either. My main problem is that I cannot get the two to work together at all when there are any lazy instantiation sets involved. I am trying to figure out how to make it work with and without loading the lazy data, but in this case I have loaded the lazy data and I am getting a "java.lang.StackOverflowError." I have two tables, "Departments," with two entries and "Employees," with three; I am using the Struts2 "xslt" result type. Here are the two persistence classes:
Departments:
package com.test.model;
// Generated Apr 7, 2012 7:10:28 PM by Hibernate Tools 3.4.0.CR1
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* Departments generated by hbm2java
*/
@Entity
@Table(name="Departments"
,catalog="test"
)
public class Departments implements java.io.Serializable {
private Integer id;
private String name;
private Set<Employees> employeeses = new HashSet(0);
public Departments() {
}
public Departments(String name) {
this.name = name;
}
public Departments(String name, Set employeeses) {
this.name = name;
this.employeeses = employeeses;
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="Id", unique=true, nullable=false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="Name", nullable=false)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="departments")
public Set<Employees> getEmployeeses() {
return this.employeeses;
}
public void setEmployeeses(Set employeeses) {
this.employeeses = employeeses;
}
}
Employees:
package com.test.model;
// Generated Apr 7, 2012 7:10:28 PM by Hibernate Tools 3.4.0.CR1
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
* Employees generated by hbm2java
*/
@Entity
@Table(name="Employees"
,catalog="test"
)
public class Employees implements java.io.Serializable {
private Integer id;
private Departments departments;
private String firstName;
private String lastName;
public Employees() {
}
public Employees(Departments departments, String firstName, String lastName) {
this.departments = departments;
this.firstName = firstName;
this.lastName = lastName;
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="Id", unique=true, nullable=false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="DepartmentsId", nullable=false)
public Departments getDepartments() {
return this.departments;
}
public void setDepartments(Departments departments) {
this.departments = departments;
}
@Column(name="FirstName", nullable=false)
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name="LastName", nullable=false)
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
The HQL query I am using in the DepartmentsManager class is: "from Departments d left join fetch d.employeeses".
Here is the stacktrace of the error:
Exception in thread "http-bio-8080-exec-4" java.lang.StackOverflowError at java.security.AccessController.doPrivileged(Native Method) at org.apache.commons.logging.LogFactory.getContextClassLoaderInternal(LogFactory.java:859) at org.apache.commons.logging.LogFactory.getFactory(LogFactory.java:423) at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:685) at com.opensymphony.xwork2.util.logging.commons.CommonsLoggerFactory.getLoggerImpl(CommonsLoggerFactory.java:29) at com.opensymphony.xwork2.util.logging.LoggerFactory.getLogger(LoggerFactory.java:42) at org.apache.struts2.views.xslt.AbstractAdapterNode.(AbstractAdapterNode.java:85) at org.apache.struts2.views.xslt.AbstractAdapterElement.(AbstractAdapterElement.java:41) at org.apache.struts2.views.xslt.BeanAdapter.(BeanAdapter.java:73) at sun.reflect.GeneratedConstructorAccessor14.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:525) at java.lang.Class.newInstance0(Class.java:372) at java.lang.Class.newInstance(Class.java:325) at org.apache.struts2.views.xslt.AdapterFactory.constructAdapterInstance(AdapterFactory.java:209) at org.apache.struts2.views.xslt.AdapterFactory.adaptNode(AdapterFactory.java:159) at org.apache.struts2.views.xslt.BeanAdapter.buildChildAdapters(BeanAdapter.java:135) at org.apache.struts2.views.xslt.AbstractAdapterNode.getChildAdapters(AbstractAdapterNode.java:128) at org.apache.struts2.views.xslt.AbstractAdapterNode.getChildNodes(AbstractAdapterNode.java:186) at org.apache.struts2.views.xslt.BeanAdapter.getChildNodes(BeanAdapter.java:88) at org.apache.struts2.views.xslt.AbstractAdapterNode.getFirstChild(AbstractAdapterNode.java:194) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2SAX.parse(DOM2SAX.java:300) at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2SAX.parse(DOM2SAX.java:302)...[this line repeats ad nauseam]
Would someone please point me in the right direction?
Thanks!
Upvotes: 1
Views: 840
Reputation: 308743
Yes, here's a thought:
You have a Departments class (bad naming - use the singular Department). It has a Set of Employee instances. It's a one to many relationship: a Department can have one or more Employees. So far, so good.
Your Employees class (more bad naming - use the singular Employee) has a reference to its parent Department.
You see the problem? Every time you call the Employee constructor, you can the Department constructor, which creates a Set of Employees. The instance of the original Employee is in that Set, which again calls the Department constructor, ad nauseum.
You need to break the cycle: it's a bidirectional one-to-many:
Upvotes: 1