coolair
V2EX  ›  问与答

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

  •  
  •   coolair · Oct 13, 2023 · 1133 views
    This topic created in 1119 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
    coolair
    OP
    Oct 13, 2023
    1
    上面代码不完整。

    ```
    @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
    Dockerfile
    Oct 13, 2023
    2
    这不是 jackson 的注解么
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2850 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:59 · PVG 18:59 · LAX 02:59 · JFK 05:59
    ♥ Do have faith in what you're doing.