我在使用 Struts2 和 Hibernate 获得具有许多表关系的大型应用程序时遇到了一些问题,因此我决定创建一个规模小得多的示例来解决这个问题,但我也无法使其正常工作。我的主要问题是,当涉及到任何惰性实例化集时,我根本无法让两者一起工作。我试图弄清楚如何让它在加载和不加载惰性数据的情况下工作,但在这种情况下,我已经加载了惰性数据并且我得到了一个“java.lang.StackOverflowError”。我有两个表,“部门”有两个条目,“员工”有三个;我正在使用 Struts2“xslt”结果类型。这是两个持久性类:
部门:
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;
}
}
雇员:
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;
}
}
我在 DepartmentsManager 类中使用的 HQL 查询是:“from Departments d left join fetch d.employeeses”。
这是错误的堆栈跟踪:
org.apache.commons.logging.LogFactory.getContextClassLoaderInternal(LogFactory.java:859) 的 java.security.AccessController.doPrivileged(Native Method) 的线程“http-bio-8080-exec-4”java.lang.StackOverflowError 中的异常在 org.apache.commons.logging.LogFactory.getFactory(LogFactory.java:423) 在 org.apache.commons.logging.LogFactory.getLog(LogFactory.java:685) 在 com.opensymphony.xwork2.util.logging.commons .CommonsLoggerFactory.getLoggerImpl(CommonsLoggerFactory.java:29) 在 com.opensymphony.xwork2.util.logging.LoggerFactory.getLogger(LoggerFactory.java:42) 在 org.apache.struts2.views.xslt.AbstractAdapterNode.(AbstractAdapterNode.java: 85) 在 org.apache.struts2.views.xslt.BeanAdapter 的 org.apache.struts2.views.xslt.AbstractAdapterElement.(AbstractAdapterElement.java:41)。(BeanAdapter.java:73) 在 sun.reflect.GeneratedConstructorAccessor14.newInstance(Unknown Source) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:525) ) 在 java.lang.Class.newInstance0(Class.java:372) 在 java.lang.Class.newInstance(Class.java:325) 在 org.apache.struts2.views.xslt.AdapterFactory.constructAdapterInstance(AdapterFactory.java: 209) 在 org.apache.struts2.views.xslt.BeanAdapter.buildChildAdapters(BeanAdapter.java:135) 在 org.apache.struts2 的 org.apache.struts2.views.xslt.AdapterFactory.adaptNode(AdapterFactory.java:159) .views.xslt.AbstractAdapterNode.getChildAdapters(AbstractAdapterNode.java:128) 在 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)在 com.sun.org.apache.xalan.internal.xsltc.trax.DOM2SAX.parse(DOM2SAX.java:300) 在 com.sun.org.apache.xalan.internal.xsltc.trax.DOM2SAX.parse(DOM2SAX. java:302)...[此行重复令人作呕]
有人能指出我正确的方向吗?
谢谢!