Hibernate中自引用类的问题

时间:2010-12-03 14:33:51

标签: hibernate spring jsf many-to-one

我有一个为父/子结构建模的实体,这是我的hibernate映射文件:

<hibernate-mapping>
    <class name="myPackage.entities.Module"  table="MODULE">
        <id name="idModule" type="int">
            <column name="ID_MODULE" precision="6" scale="0" />
            <generator class="sequence">
                <param name="sequence">S_MODULE</param>
            </generator>
        </id>
        <property generated="never" lazy="false" name="nomModule"
            type="string">
            <column length="30" name="NOM_MODULE" />
        </property>
        ...
        <!-----------------Childs------------------------->         
        <set fetch="select" inverse="true" lazy="false" name="subModules"
            sort="unsorted" table="MODULE">
            <key>
                <column name="MOD_ID_MODULE" />
            </key>
            <one-to-many class="myPackage.entities.Module" />
        </set>
        <!-----------------parent------------------------->

        <many-to-one class="myPackage.entities.Module" name="parentModule">
            <column name="MOD_ID_MODULE"  />
        </many-to-one>

    </class>
</hibernate-mapping>

这是数据:

"ID_MODULE" "ID_APPLICATION" "MOD_ID_MODULE"(parent)  ...
"12"             "2"             "10"            
"10"             "2"             "3"     
"14"             "2"             "12"   
"1"                  "1"              ""    
"2"                  "1"             "1"    
"3"                  "2"             "" 
"4"                  "2"             "3"    
"13"             "2"             "4"    
"6"                  "1"             "2"    

我的问题是每当我尝试在视图中显示对象时,多对一关联都会生成异常:

javax.servlet.ServletException: L'exécution de la servlet a lancé une exception
    org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206)
    org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
    org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)
    org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

cause mère

java.lang.StackOverflowError
    java.lang.Exception.<init>(Exception.java:77)
    java.lang.reflect.InvocationTargetException.<init>(InvocationTargetException.java:54)
    sun.reflect.GeneratedMethodAccessor150.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1773)
    org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1132)
    org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:686)
    org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:715)
    org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:290)
    org.ajax4jsf.javascript.ScriptUtils.writeScriptToStream(ScriptUtils.java:126)
    org.ajax4jsf.javascript.ScriptUtils.writeScriptToStream(ScriptUtils.java:174)
    org.ajax4jsf.javascript.ScriptUtils.writeScriptToStream(ScriptUtils.java:174)
    org.ajax4jsf.javascript.ScriptUtils.writeScriptToStream(ScriptUtils.java:174)
    org.ajax4jsf.javascript.ScriptUtils.writeScriptToStream(ScriptUtils.java:174)
    org.ajax4jsf.javascript.ScriptUtils.writeScriptToStream(ScriptUtils.java:83)
    org.ajax4jsf.javascript.ScriptUtils.writeScriptToStream(ScriptUtils.java:174)
    org.ajax4jsf.javascript.ScriptUtils.writeScriptToStream(ScriptUtils.java:174)
    org.ajax4jsf.javascript.ScriptUtils.writeScriptToStream(ScriptUtils.java:174)
    org.ajax4jsf.javascript.ScriptUtils.writeScriptToStream(ScriptUtils.java:174)
    org.ajax4jsf.javascript.ScriptUtils.writeScriptToStream(ScriptUtils.java:83)
    org.ajax4jsf.javascript.ScriptUtils.writeScriptToStream(ScriptUtils.java:174)
    org.ajax4jsf.javascript.ScriptUtils.writeScriptToStream(ScriptUtils.java:174)
    org.ajax4jsf.javascript.ScriptUtils.writeScriptToStream(ScriptUtils.java:174)
    org.ajax4jsf.javascript.ScriptUtils.writeScriptToStream(ScriptUtils.java:174)
    org.ajax4jsf.javascript.ScriptUtils.writeScriptToStream(ScriptUtils.java:83)
...

注意:我使用Hibernate,Spring和JSF

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

显然,Ajax4JSF尝试将您的数据序列化为Javascript并进入无限递归,因为您的对象图是循环的(父对象指向子级,子级指向父级)。也许您需要找到一种方法从Ajax4JSF序列化中排除“子到父”关系。

答案 1 :(得分:0)

它看起来像一个无限循环。您确定您的数据不包含自己孩子的父母吗?它也可以是 - &gt; b和b - &gt; a或任何其他周期。