ssh+ext使用jsonplugin-0.32.jar返回json格式,extends="json-default",type="json"
单表OK,
一对多关联时,是否可以取得相应关联对象信息?
如:User 与 Role关联,获得用户user,是否可以同时取得关联的Role信息?JSON格式?
THX!

解决方案 »

  1.   

    <set name="testUsers" inverse="true" cascade="all" lazy="false">
    <key>
    <column name="role_id" not-null="true" />
    </key>
    <one-to-many class="com.onlyfun.model.TestUser" />
    </set>
    SQL有发出,数据也能取得,转化成JSON时报错,如下:
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.onlyfun.model.User.role, no session or session was closed
      

  2.   

    使用Jackson转化时也是一样的错本地数据测试都是OK的,如下
    {"myBeans":[{"birthday":1291114593620,"age":10,"name":"name0"},{"birthday":1291114593620,"age":11,"name":"name1"}],"id":1}
      

  3.   

    实际数据就是JAVA BEAN ,数据都对的。 转化成JSON时报这个异常。无法转化对应的ROLE信息。SESSION关闭。
      

  4.   

    我怀疑是类对象里还有一个对象,json无法转换
      

  5.   

    一对多时。
    无数据库交互时,是可以转化。
    与数据库交互时,报上面的异常。
    想在后台转化成JSON格式传至前台。
    单表的话也是可以的。
    关联时转化出现异常。不知道要如何解决。
      

  6.   

    <set name="testUsers" inverse="true" cascade="all" not-fount="ignore" lazy="false">
    加上not-found试试呢
      

  7.   

    many-to-one里加 not-fount="ignore"
      

  8.   

    public class TestUser implements java.io.Serializable { // Fields private Integer userId;
    private TestRole testRole;
    private String userName;
    private Date birthday;

    }<hibernate-mapping>
        <class name="com.onlyfun.model.TestUser" table="test_user" catalog="extfun">
            <id name="userId" type="java.lang.Integer">
                <column name="user_id" />
                <generator class="native" />
            </id>
            <many-to-one name="testRole" class="com.onlyfun.model.TestRole" fetch="select">
                <column name="role_id" not-null="true" />
            </many-to-one>
    public class TestRole implements java.io.Serializable { // Fields private Integer roleId;
    private String roleName;
    private Set testUsers = new HashSet(0);

    }<class name="com.onlyfun.model.TestRole" table="test_role"
    lazy="false" catalog="extfun">
    <id name="roleId" type="java.lang.Integer">
    <column name="role_id" />
    <generator class="native" />
    </id>
    <property name="roleName" type="java.lang.String">
    <column name="role_name" length="20" not-null="true" />
    </property>
    <set name="testUsers" inverse="true" cascade="all" lazy="false">
    <key>
    <column name="role_id" not-null="true" />
    </key>
    <one-to-many class="com.onlyfun.model.TestUser" />
    </set>
    DAO/**
     * 获得所有对象
     * 
     * @param clazz
     * @return
     */
    public List getObjects(Class clazz) {
    return getHibernateTemplate().loadAll(clazz);
    }测试类:
    public void test() { TestUserService testUserService = (TestUserService) applicationContext
    .getBean("testUserService"); //获得testUser List
    List<TestUser> list = testUserService.findAllTestUser(); if (list != null && list.size() > 0) {
    for (TestUser user : list) {
    System.out.println(user.getUserName() + "\t"
    + user.getBirthday() + "\t" + user.getUserName() + "\t"
    + user.getTestRole().getRoleName() + "\t"
    + user.getTestRole().getRoleId());
    //数据都有
    } } PageVo pageVo = new PageVo();
    pageVo.setList(list); // get instance
    ObjectMapper mapper = JacksonMapper.getInstance();

    //bean to json
    try {
    StringWriter sw = new StringWriter();
    JsonGenerator gen;
    gen = new JsonFactory().createJsonGenerator(sw);
    mapper.writeValue(gen, pageVo);
    gen.close();
    String json = sw.toString();//转化时报错。
    System.out.println(json);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } }
      

  9.   

    THX 两位。not-fount="ignore"添加后
    还是一样的异常。 SESSION关闭的原因
    请问有没什么解决办法呢。THX!
      

  10.   

    你这个我没写过我用的是JsonArray.fromObject(list)String json = "";
    json = JSONArray.fromObject(list).toString(1);
    System.out.println(json);
    out.print(json);
      

  11.   

    not-fount="ignore"我打错了是not-found="ignore"
      

  12.   

    我添加的也是not-found="ignore" 试了是一样的错。
    jackson的方法,自已练习的时候学的。
    项目中用的是jsonplugin。但关联时都是一样的错。
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.onlyfun.model.User.role, no session or session was closed
      

  13.   

    首先你的List集合可以输出值,说明取值没问题
    其次可以报错,说明问题在try块里//bean to json
            try {
                StringWriter sw = new StringWriter();
                JsonGenerator gen;
                gen = new JsonFactory().createJsonGenerator(sw);
                mapper.writeValue(gen, pageVo);
                gen.close();
                String json = sw.toString();//转化时报错。
                System.out.println(json);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }再次,jsonplugin我没用过,不清楚工作原理,到底哪里涉及到session,我也不清楚.
    不如你自己写一个获取session的方法 try开始打开
    finally关闭
      

  14.   

    <many-to-one name="testRole" class="com.onlyfun.model.TestRole" fetch="select">
    这里加上lazy="false"估计可以了吧,如果还不行,我也没什么办法了
      

  15.   


    在web里面配置
    <filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
    试试
      

  16.   

    web.xml有加的。。<filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    </filter-class>
    </filter>
    <filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping> <filter>
    <filter-name>testJackson</filter-name>
    <filter-class>
    org.apache.struts2.dispatcher.FilterDispatcher
    </filter-class>
    </filter>
    <filter-mapping>
    <filter-name>testJackson</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping> <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
    </context-param>