简体   繁体   English

无法写入 JSON:未能一对一延迟初始化角色集合

[英]Could not write JSON: failed to lazily initialize a collection of role one to one

I have the following code... It's an application where the user has a wall, and on that wall there can be several games.我有以下代码......这是一个用户有一堵墙的应用程序,在那堵墙上可以有几个游戏。

@Entity
@Table(name = "tb_user")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;

    @OneToOne(mappedBy = "user",fetch = FetchType.EAGER)
    private Mural mural;

    public User() {

    }

    public User(Long id, String name, Mural mural) {
        this.id = id;
        this.name = name;
        this.mural = mural;

     //getters and setters...
    }
@Entity
@Table(name = "tb_mural")
public class Mural implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;

    @ManyToMany
    @JoinTable(name = "tb_mural_games",
            joinColumns = @JoinColumn(name = "mural_id"),
            inverseJoinColumns = @JoinColumn(name = "games_id"))
    private Set<Game> games = new HashSet<>();

    @OneToOne
    @JoinColumn(name = "user_id")
    private User user;

    public Mural() {

    }

    public Mural(Long id, String name, User user) {
        this.id = id;
        this.name = name;
        this.user = user;
    }
 
    //getters and setters...


And the following request...以下请求...

 @GetMapping
    public ResponseEntity<List<User>> findAll(){
        List<User> list = repository.findAll();
        return ResponseEntity.ok().body(list);
    }

But I have the following error...但我有以下错误...

 Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role... continue...

I tried using FETCH.EAGER but it doesn't work, it only works if I use jsonignore, but I would like it to return the "mural" in the user request.我尝试使用 FETCH.EAGER 但它不起作用,它只有在我使用 jsonignore 时才有效,但我希望它在用户请求中返回“壁画”。

Put the JsonIgnore in the Mural class on将 JsonIgnore 放在壁画 class 上

@OneToOne
@JoinColumn(name = "user_id")
private User user;

Also try to avoid directly returning entities to the end user.还要尽量避免直接将实体返回给最终用户。 Either try to map the entity to DTO or use projection ( https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections ).尝试将实体 map 转换为 DTO 或使用投影( https://docs.spring.io/spring-data#/jpa/docs/current/referenceshtml )。

暂无
暂无

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

相关问题 无法写入 JSON:未能延迟初始化角色集合: - Could not write JSON: failed to lazily initialize a collection of role: 无法写入 JSON:未能延迟初始化角色集合 - Could not write JSON: failed to lazily initialize a collection of role 无法编写JSON:无法延迟初始化集合 - Could not write JSON: failed to lazily initialize a collection 延迟获取具有多个关系的实体:无法写入 JSON:未能延迟初始化角色集合 - Lazily fetching entity with multiple relations: Could not write JSON: failed to lazily initialize a collection of role 无法编写内容:无法使用OpenEntityManagerInViewFilter延迟初始化角色集合 - Could not write content: failed to lazily initialize a collection of role using OpenEntityManagerInViewFilter 使用 springboot 和 hibernate 搜索“无法编写 JSON:无法延迟初始化角色集合” - "Could not write JSON: failed to lazily initialize a collection of role" with springboot and hibernate search 无法编写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 休眠-无法延迟初始化角色集合:无法初始化代理-没有会话 - Hibernate - failed to lazily initialize a collection of role: could not initialize proxy - no Session 无法延迟初始化角色集合无法初始化代理-无会话 - Failed to lazily initialize a collection of role could not initialize proxy - no Session 无法延迟初始化角色集合:无法初始化代理-无会话 - failed to lazily initialize a collection of role : could not initialize proxy - no Session
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM