尝试使用/ Hibernate示例运行简单的Struts2时出错

时间:2012-04-11 00:21:02

标签: java hibernate xslt struts2 persistence

我在使用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查询是:“来自Departments d left join fetch d.employeeses”。

以下是错误的堆栈跟踪:

  

线程中的异常“http-bio-8080-exec-4”java.lang.StackOverflowError       at java.security.AccessController.doPrivileged(Native Method)       在org.apache.commons.logging.LogFactory.getContextClassLoaderInternal(LogFactory.java:859)       在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.AbstractAdapterElement。(AbstractAdapterElement.java:41)       在org.apache.struts2.views.xslt.BeanAdapter。(BeanAdapter.java:73)       at sun.reflect.GeneratedConstructorAccessor14.newInstance(Unknown Source)       at 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.AdapterFactory.adaptNode(AdapterFactory.java:159)       在org.apache.struts2.views.xslt.BeanAdapter.buildChildAdapters(BeanAdapter.java:135)       at org.apache.struts2.views.xslt.AbstractAdapterNode.getChildAdapters(AbstractAdapterNode.java:128)       在org.apache.struts2.views.xslt.AbstractAdapterNode.getChildNodes(AbstractAdapterNode.java:186)       在org.apache.struts2.views.xslt.BeanAdapter.getChildNodes(BeanAdapter.java:88)       在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)... [此行重复广告恶心]

有人请指出我正确的方向吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

是的,这是一个想法:

你有一个Departments类(命名错误 - 使用单数部门)。它有一组Employee实例。这是一对多的关系:一个部门可以有一个或多个员工。到目前为止,非常好。

您的员工类(更糟糕的命名 - 使用单数员工)具有对其父部门的引用。

你看到了问题吗?每次调用Employee构造函数时,都可以使用Department构造函数创建一组Employees。原始Employee的实例位于该Set中,该Set再次调用Department构造函数,并且令人作呕。

你需要打破这个循环:它是双向的一对多:

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/example-parentchild.html#example-parentchild-bidir