JsonMappingException: failed to lazily initialize a collection of role

3 篇文章 0 订阅
2 篇文章 0 订阅
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")
	@JsonIgnore
	private 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)
	@ResponseBody
	public 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值