尝试将条件列表结果转换为json时出现StackOverflowError

时间:2014-09-02 10:51:18

标签: java json hibernate criteria

我创建了一个将Hibernate条件列表结果转换为json的应用程序,首先我尝试了Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter

我解决了,当我用谷歌搜索时,我遇到了这些东西 Could not serialize object cause of HibernateProxy

现在我收到如下所示的StackOverflowError

INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: 
    select
        this_.id as id1_1_0_,
        this_.base_id as base_id2_1_0_,
        this_.name as name3_1_0_ 
    from
        testdatabase12.child this_
Hibernate: 
    select
        base0_.id as id1_0_0_,
        base0_.name as name2_0_0_ 
    from
        testdatabase12.base base0_ 
    where
        base0_.id=?
Hibernate: 
    select
        childs0_.base_id as base_id2_0_0_,
        childs0_.id as id1_1_0_,
        childs0_.id as id1_1_1_,
        childs0_.base_id as base_id2_1_1_,
        childs0_.name as name3_1_1_ 
    from
        testdatabase12.child childs0_ 
    where
        childs0_.base_id=?
Exception in thread "main" java.lang.StackOverflowError
    at java.util.LinkedHashMap$LinkedHashIterator.<init>(LinkedHashMap.java:345)
    at java.util.LinkedHashMap$LinkedHashIterator.<init>(LinkedHashMap.java:345)
    at java.util.LinkedHashMap$ValueIterator.<init>(LinkedHashMap.java:387)
    at java.util.LinkedHashMap$ValueIterator.<init>(LinkedHashMap.java:387)
    at java.util.LinkedHashMap.newValueIterator(LinkedHashMap.java:397)
    at java.util.HashMap$Values.iterator(HashMap.java:910)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:203)
    at com.dao.HibernateProxyTypeAdapter.write(HibernateProxyTypeAdapter.java:52)
    at com.dao.HibernateProxyTypeAdapter.write(HibernateProxyTypeAdapter.java:1)
:
:

我的代码如下所示

App.java

public class App {
    public static void main(String[] args) {

        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session session = sf.openSession();
        List names = session.createCriteria(Child.class).list();
        GsonBuilder b = new GsonBuilder();

        b.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);

        Gson gson = b.create();
            String jsonNames = gson.toJson(names);
            System.out.println("jsonNames = " + jsonNames);
        session.close();

    }
}

Base.java

public class Base implements java.io.Serializable {

    private Integer id;
    private String name;
    private Set childs = new HashSet(0);

    // getters and setters
}

Base.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 2, 2014 5:23:25 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.mappings.Base" table="base" catalog="testdatabase12">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="name" type="string">
            <column name="name" length="20" />
        </property>
        <set name="childs" table="child" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="base_id" />
            </key>
            <one-to-many class="com.mappings.Child" />
        </set>
    </class>
</hibernate-mapping>

Child.java

public class Child implements java.io.Serializable {

    private Integer id;
    private Base base;
    private String name;

   //getters and setters

}

Child.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 2, 2014 5:23:25 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.mappings.Child" table="child" catalog="testdatabase12">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <many-to-one name="base" class="com.mappings.Base" fetch="select">
            <column name="base_id" />
        </many-to-one>
        <property name="name" type="string">
            <column name="name" length="20" />
        </property>
    </class>
</hibernate-mapping>

1 个答案:

答案 0 :(得分:0)

我认为这是关于List的。您可以使用ArrayList从List中生成JSON。

试试这个代码我不确定但是当你清除红色标记时它会起作用。

public class App {
    public static void main(String[] args) {

        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session session = sf.openSession();
        List names = session.createCriteria(Child.class).list();
        GsonBuilder b = new GsonBuilder();

        b.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY);

        Gson gson = new Gson();
//seperate adding
        gson.toJson("propertyName" , List.get(0).getter...());
        gson.toJson("propertyName" , List.get(1).getter...());


        session.close();

    }
}