ssh+ext使用jsonplugin-0.32.jar返回json格式,extends="json-default",type="json"
单表OK,
一对多关联时,是否可以取得相应关联对象信息?
如:User 与 Role关联,获得用户user,是否可以同时取得关联的Role信息?JSON格式?
THX!
单表OK,
一对多关联时,是否可以取得相应关联对象信息?
如:User 与 Role关联,获得用户user,是否可以同时取得关联的Role信息?JSON格式?
THX!
解决方案 »
- jsp <%@ include file="send.inc.jsp" %> 后提示包异常不能编译成class文件
- 【急】JSP做RTX的短信二次开发
- Java.lang.NullPointerException 的问题
- checkbox选择数量问题
- 实现双机备份?
- 问大家一个标准做法
- memcached怎样移除down的节点
- 数据库与中文问题!
- jsp用户登陆的小问题。初学请给位高手指点。给分!!!在线等待!!!
- 用JB建了个EJB(一句代码也没添加),运行时出错,为什么?(好几天了,还没解决)
- itext包导出PDF的数据显示不全问题
- HTTP method GET is not supported by this URL
<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
{"myBeans":[{"birthday":1291114593620,"age":10,"name":"name0"},{"birthday":1291114593620,"age":11,"name":"name1"}],"id":1}
无数据库交互时,是可以转化。
与数据库交互时,报上面的异常。
想在后台转化成JSON格式传至前台。
单表的话也是可以的。
关联时转化出现异常。不知道要如何解决。
加上not-found试试呢
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();
} }
还是一样的异常。 SESSION关闭的原因
请问有没什么解决办法呢。THX!
json = JSONArray.fromObject(list).toString(1);
System.out.println(json);
out.print(json);
jackson的方法,自已练习的时候学的。
项目中用的是jsonplugin。但关联时都是一样的错。
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.onlyfun.model.User.role, no session or session was closed
其次可以报错,说明问题在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关闭
这里加上lazy="false"估计可以了吧,如果还不行,我也没什么办法了
在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>
试试
<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>