简体   繁体   中英

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:

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 :

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)

If I remove the cars set in the user mapping the error goes away and I am able to convert the user to 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. I solved this problem here:

Hibernate Object not detaching

Answer : pull object from hibernate. Create separate java object. Loop through objects and populate newly created objects with hibernate object values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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