JsonMappingException: failed to lazily initialize a collection of role

来源:互联网 发布:中国建设网络强国优势 编辑:程序博客网 时间:2024/04/29 19:53
package com.ln.baseframe.security.entity;import java.util.Set;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.JoinColumn;import javax.persistence.ManyToOne;import javax.persistence.OneToMany;import javax.persistence.Table;import com.fasterxml.jackson.annotation.JsonIgnore;import com.ln.baseframe.common.entity.IdEntity1;@Entity@Table(name="t_module")public class Module extends IdEntity1{@Column(length=50)private String description;@Column(length=20)private String name;private String url;private String sn;//优先级private Integer priority;public Integer getPriority() {return priority;}public void setPriority(Integer priority) {this.priority = priority;}@ManyToOne(fetch=FetchType.LAZY)@JoinColumn(name="parent_id",referencedColumnName="id")@JsonIgnoreprivate Module parent;@OneToMany(fetch=FetchType.LAZY,mappedBy="parent")private Set<Module> modules;public Module getParent() {return parent;}public void setParent(Module parent) {this.parent = parent;}public Set<Module> getModules() {return modules;}public void setModules(Set<Module> modules) {this.modules = modules;}public String getSn() {return sn;}public void setSn(String sn) {this.sn = sn;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}}


Controller

@RequestMapping(value = "leftMenuData",method = RequestMethod.POST)@ResponseBodypublic Object leftMenuData(){List<Module> modules = moduleService.queryFirstAndSecondModule();for (Module module : modules) {System.out.println(module.getName());for (Module children : module.getModules()) {System.out.println(children.getName());}}return modules;}


Service

public List<Module> queryFirstAndSecondModule(){Module parent = new Module();parent.setId(5L);List<Module> modules = baseDao.queryByHql("select distinct m from Module m left join fetch m.modules where m.parent = ? ", parent);return modules;}


页面请求leftMenuData,报错:com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.ln.baseframe.security.entity.Module.modules, could not initialize proxy - no Session


使用junit进行单元测试,代码如下,不会报错。在这里事务已经关闭,但queryFirstAndSecondModule使用的是迫切左连接加载,所以getModules时不会报懒加载异常。

public void testQueryFirstAndSecondModule() throws JsonProcessingException {List<Module> modules = moduleService.queryFirstAndSecondModule();for (Module module : modules) {System.out.println(module.getName());for (Module children : module.getModules()) {System.out.println(children.getName());}}}

然后再加入如下代码,就会报错:com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.ln.baseframe.security.entity.Module.modules, could not initialize proxy - no Session
ObjectMapper objectMapper = new ObjectMapper();String result = objectMapper.writeValueAsString(modules);System.out.println(result);
解决方法加入如下代码
objectMapper.registerModule(new Hibernate4Module());
<dependency>    <groupId>com.fasterxml.jackson.datatype</groupId>    <artifactId>jackson-datatype-hibernate4</artifactId>    <version>2.3.2</version></dependency>

web端有两种解决方式

方式一

public class HibernateAwareObjectMapper extends ObjectMapper {    public HibernateAwareObjectMapper() {        registerModule(new Hibernate4Module());    }}
<mvc:annotation-driven>        <mvc:message-converters>            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">            <property name="supportedMediaTypes">              <list>                  <value>text/html;charset=UTF-8</value>              </list>          </property>                  <property name="objectMapper">                <bean class="com.ln.baseframe.common.utils.HibernateAwareObjectMapper" />                </property>            </bean>        </mvc:message-converters>    </mvc:annotation-driven>
方式二

<mvc:annotation-driven>        <mvc:message-converters>            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">            <property name="supportedMediaTypes">              <list>                  <value>text/html;charset=UTF-8</value>              </list>          </property>                  <property name="objectMapper">                <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">   <property name="modulesToInstall" value="com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module"/></bean>                </property>            </bean>        </mvc:message-converters>    </mvc:annotation-driven>






0 0
原创粉丝点击