1

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!

J Ellis
  • 668
  • 3
  • 8
  • 20

1 Answers1

1

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:

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

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • That makes a lot of sense! If I remove the "Departments" object from "Employees" I get exactly what I want! However, do I lose some functionality not having "Employees" mapped back to "Departments"? If so, is there a better way to do it (like through an annotation)? Thanks! – J Ellis Apr 11 '12 at 20:19
  • Look at the Hibernate docs that I sent you. They have a section on how to do bi-directional one-to-many properly. Give it a read and see if you missed something important. – duffymo Apr 11 '12 at 20:22
  • I guess the disconnect I am having is that the link you provided uses xml mapping, while I am using annotations. I tried to use the bidirectional instructions in this link: http://docs.redhat.com/docs/en-US/JBoss_Enterprise_Application_Platform/4.2/html-single/Hibernate_Annotations_Reference_Guide/index.html but it produces the exact same problem, an endless loop. – J Ellis Apr 11 '12 at 20:26
  • To clarify the above comment, I changed the annotations in Departments to "@OneToMany" and "@JoinColumn(name="DepartmentsId", nullable=false)" and in Employees to "@ManyToOne" and "@JoinColumn(name="DepartmentsId", nullable=false, insertable=false, updatable=false)" as described in the Hibernate documentation (http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-hibspec-collection), but it still is not breaking the cycle. – J Ellis Apr 11 '12 at 20:36
  • You need "inverse=true" on the many side: http://stackoverflow.com/questions/1061179/when-to-use-inverse-false-on-nhibernate-hibernate-onetomany-relationships – duffymo Apr 11 '12 at 20:45
  • Thank you for your help. I appreciate what you are saying, but according to what I am reading, the annotations I am using are supposed to set the inverse variable. Your information has been a lot of help though, and has really put me on the right path. – J Ellis Apr 11 '12 at 21:15