V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
coolair
V2EX  ›  问与答

@JsonIgnore 在 Spring Boot 3.1.4 中无效了吗?

  •  
  •   coolair · Oct 13, 2023 · 780 views
    This topic created in 938 days ago, the information mentioned may be changed or developed.

    使用了 JPA 、Lombok 代码如下:

    @Data
    @Entity
    public class User {
        private List<Role> roles;
    }
    
    @Data
    @Entity
    public class Role {
        @JsonIgnore
        @ManyToMany(mappedBy = "roles")
        List<User> users;
    }
    

    同样的代码在 Spring Boot 2.7.16 中没问题,在 Spring Boot 3.1.4 中就不行了,是哪里没写对吗?

    coolair
        1
    coolair  
    OP
       Oct 13, 2023
    上面代码不完整。

    ```
    @Data
    @Entity
    public class User {
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
    name = "user_role",
    joinColumns = @JoinColumn(name = "user_id"),
    inverseJoinColumns = @JoinColumn(name = "role_id")
    )
    private List<Role> roles;
    }

    @Data
    @Entity
    public class Role {
    @JsonIgnore
    @ManyToMany(mappedBy = "roles")
    List<User> users;
    }
    ```

    刚在 Spring Boot 2.7.16 测了,也不行。貌似只能用 `@Transient` 了?
    Dockerfile
        2
    Dockerfile  
       Oct 13, 2023
    这不是 jackson 的注解么
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   971 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 927ms · UTC 20:05 · PVG 04:05 · LAX 13:05 · JFK 16:05
    ♥ Do have faith in what you're doing.