简体   繁体   English

Spring MVC:无法初始化代理-没有会话(通过引用链)

[英]Spring MVC :could not initialize proxy - no Session (through reference chain)

type Exception report 类型例外报告

message Could not write content: failed to lazily initialize a collection of role: edu.waa.classified.dto.User.products, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->edu.waa.classified.dto.User["products"]); 消息无法写入内容:无法延迟初始化角色集合:edu.waa.classified.dto.User.products,无法初始化代理-无会话(通过参考链:java.util.ArrayList [0]-> edu .waa.classified.dto.User [“ products”]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: edu.waa.classified.dto.User.products, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->edu.waa.classified.dto.User["products"]) 嵌套的异常是com.fasterxml.jackson.databind.JsonMappingException:无法延迟初始化角色集合:edu.waa.classified.dto.User.products,无法初始化代理-没有会话(通过参考链:java.util。 ArrayList [0]-> edu.waa.classified.dto.User [“ products”])

description The server encountered an internal error that prevented it from fulfilling this request. 描述服务器遇到内部错误,导致服务器无法满足此请求。

I just added the @JsonIgnore at the after the @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) annotations. 我只是在@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)批注之后添加了@JsonIgnore

It worked, but not sure why it worked. 它起作用了,但是不确定为什么起作用。

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "WISHLIST", joinColumns = {
@JoinColumn(name = "userId", referencedColumnName = "id") }, inverseJoinColumns = {
@JoinColumn(name = "productId", referencedColumnName = "id") })
@JsonIgnore
private List<Product> products;

The problem is your FetchType.lazy. 问题是您的FetchType.lazy。 When Jackson converts the User Entity, it trys to load the products but the hibernate session is closed. 当Jackson转换用户实体时,它将尝试加载产品,但是休眠会话已关闭。

So adding @JsonIgnore is one way to solve this but when you need the products returned as well, this is no option. 因此,添加@JsonIgnore是解决此问题的一种方法,但是当您还需要退回产品时,这是没有选择的。

I found this answer which solves the same problem for me and gave me the lazy loaded type: https://stackoverflow.com/a/21760361 我找到了这个答案,它为我解决了相同的问题,并给了我惰性加载类型: https : //stackoverflow.com/a/21760361

The good thing about this solution is, that you can still use the lazy load for products. 此解决方案的好处是,您仍然可以对产品使用延迟加载。

I guess your controller is out of hibernate session. 我猜您的控制器已退出休眠会话。 (no @Transactional). (没有@Transactional)。 But your json list wants to be filled outside that session. 但是您的json列表希望在该会话之外进行填充。 But hibernate filled with proxy instead of real data, and outside hibernate session it cannot load that data when you try to access is. 但是休眠填充了代理而不是真实数据,在休眠会话之外,当您尝试访问时,它无法加载该数据。 JsonIgnore doesn't ask for that data, so it works. JsonIgnore不需要该数据,因此可以正常工作。 Or if you want that Data in your json object, do fetchtype EAGER. 或者,如果要在json对象中使用该数据,请执行fetchtype EAGER。 Then hibernate load them immediately. 然后休眠,立即加载它们。 If you want that list only in special cases leave it LAZY, but access it then on service layer in a special method , which has annotation @transactional. 如果仅在特殊情况下只需要该列表,则将其保留为LAZY,然后在服务层使用特殊方法访问它,该方法具有批注@transactional。 Then hibernate is able to fill that list. 然后休眠可以填充该列表。

I also came through the same problem. 我也遇到了同样的问题。 I just added the below annotation to my Entity class (Just before the entity class). 我只是将以下注释添加到我的Entity类中(就在实体类之前)。 It does the job for sure. 它确实可以完成这项工作。

@Entity @Table(name="Country") @Proxy(lazy = false) public class Country {}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法初始化代理 - 没有会话 - could not initialize proxy - no Session JsonMappingException:无法初始化代理 - 没有 Session - JsonMappingException: could not initialize proxy - no Session LazyInitializationException的问题无法初始化代理-没有会话 - Issue with LazyInitializationException could not initialize proxy - no Session HTTP 状态 500 - 无法延迟初始化角色集合:...,无法初始化代理 - 没有会话 - HTTP Status 500 - failed to lazily initialize a collection of role: ..., could not initialize proxy - no Session 无法编写JSON:无法延迟初始化角色集合:com.managem.model.Region.pays,无法初始化代理-没有会话 - Could not write JSON: failed to lazily initialize a collection of role: com.managem.model.Region.pays, could not initialize proxy - no Session 动态json响应:无法读取JSON:N / A(通过引用链)jackson fasterxml - dynamic json response: Could not read JSON: N/A (through reference chain) jackson fasterxml 无法延迟初始化集合,无法初始化代理用于将JSON转换为Entity - failed to lazily initialize a collection, could not initialize proxy For converting JSON to Entity JsonMappingException:(是NullPointerException)(通过参考链:com.domain.ProfitStrategyInputMsg [“ productBasicInfo”]) - JsonMappingException: (was NullPointerException) (through reference chain: com.domain.ProfitStrategyInputMsg[“productBasicInfo”]) CSV 生成器不支持属性的 Object 值(通过引用链) - CSV generator does not support Object values for properties (through reference chain) Spring MVC @Scope代理bean和Jackson 2 - Spring MVC @Scope proxy bean & Jackson 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM