简体   繁体   English

将休眠对象转换为json时出现stackoverflow错误

[英]stackoverflow error when converting hibernate object to json

I have a user object with the following hibernate mapping which is a many to many self join: 我有一个具有以下休眠映射的用户对象,该映射是多对多自连接:

    <hibernate-mapping>
        <class name="User" table="User">
            <id name="id" type="int">
            <column name="userId" />
                <generator class="native" />
            </id>
            <set name="friends" table="User_Friend" 
                    inverse="false" lazy="true" cascade="all">
                <key column="userId"/>
                <many-to-many column="friendId" class="User" />
            </set>
            <set name="cars" table="Car" inverse="true" fetch="select" lazy="true">
            <key>
                <column name="userId" not-null="true" />
            </key>
            <one-to-many class="Car" />
        </set>
        </class>
    </hibernate-mapping>

The car mapping looks like the following: 汽车映射如下所示:

<hibernate-mapping>
    <class name="Car" table="Car">
        <id name="id" type="int">
            <column name="carId" />
            <generator class="native" />
        </id>
        <set name="carStatuses" table="Car_Status" 
                inverse="true" lazy="true" fetch="select">
            <key>
                <column name="carId" not-null="true" />
            </key>
            <one-to-many class="CarStatus" />
        </set>
        <many-to-one name="user" 
        column="userId"
        not-null="true"/>

    </class>
</hibernate-mapping>

I retrieve the user object and then attempt to return it as a Restlet JSON Representation with this method: 我检索用户对象,然后尝试使用此方法将其作为Restlet JSON表示形式返回:

public Representation getJSONRepresentationFromObject(User object) {


    JSONArray ja = new JSONArray();
    JSONObject jo = new JSONObject(object);

    ja.put(jo);

    JsonRepresentation jr = new JsonRepresentation(ja);
    jr.setCharacterSet(CharacterSet.UTF_8);

    return jr;
}

The problem is that I get a StackOverflowError : 问题是我得到一个StackOverflowError

WARNING: Exception or error caught in resource java.lang.StackOverflowError at sun.reflect.GeneratedMethodAccessor74.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.json.JSONObject.populateMap(JSONObject.java:988) at org.json.JSONObject.(JSONObject.java:272) at org.json.JSONObject.wrap(JSONObject.java:1587) at org.json.JSONArray.(JSONArray.java:158) at org.json.JSONObject.wrap(JSONObject.java:1569) at org.json.JSONObject.populateMap(JSONObject.java:990) at org.json.JSONObject.(JSONObject.java:272) at org.json.JSONObject.wrap(JSONObject.java:1587) at org.json.JSONArray.(JSONArray.java:158) at org.json.JSONObject.wrap(JSONObject.java:1569) at org.json.JSONObject.populateMap(JSONObject.java:990) at org.json.JSONObject.(JSONObject.java:272) 警告:在sun.reflect.GeneratedMethodAccessor74.invoke处的资源java.lang.StackOverflowError中捕获到异常或错误(在Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)处的java.lang.reflect.Method.invoke (Method.java:597)在org.json.JSONObject.populateMap(JSONObject.java:988)在org.json.JSONObject。(JSONObject.java:272)在org.json.JSONObject.wrap(JSONObject.java:1587) )的org.json.JSONArray。(JSONArray.java:158)的org.json.JSONObject.wrap(JSONObject.java:1569)的org.json.JSONObject.populateMap(JSONObject.java:990)的org.json。 org.json.JSONObject.wrap(JSONObject.java:1587)上org.json.JSONArray。(JSONArray.java:158)上org.json.JSONObject.wrap(JSONObject.java)上的JSONObject。(JSONObject.java:272) :1569),位于org.json.JSONObject.populateMap(JSONObject.java:990),位于org.json.JSONObject。(JSONObject.java:272)

If I remove the cars set in the user mapping the error goes away and I am able to convert the user to json. 如果我删除了用户映射中设置的汽车,错误就会消失,并且能够将用户转换为json。 Is there something wrong with the car mapping that throws it into an infinite loop? 汽车映射是否存在问题,将其陷入无限循环?

The problem was that the Car object has a back reference to the User/ Friend object. 问题是Car对象具有对User / Friend对象的反向引用。 I solved this problem here: 我在这里解决了这个问题:

Hibernate Object not detaching 休眠对象未分离

Answer : pull object from hibernate. :从休眠中拉出对象。 Create separate java object. 创建单独的java对象。 Loop through objects and populate newly created objects with hibernate object values. 遍历对象,并使用休眠对象值填充新创建的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM