Spring数据全部一对多地搁置

Spring数据全部一对多地搁置,第1张

Spring数据全部一对多地搁置

终于我找到了解决方案。这是Jackson
json解组的问题。缺少的是一种告诉对象映射器一对多关系的方法,只有这样,它才能正确创建父对象和子对象,然后hibernate将在该对象上处理其余的工作。幸运的是,杰克逊有两个注释@JsonManagedReference和@JsonBackReference来告诉对象映射器有关该关系的信息。更新的代码-

User.java

@Data@Entity@Table(name = "users", uniqueConstraints={ @UniqueConstraint(columnNames={"username"})})public class User {    @Id    @CustomEmailValidator    @Size.List ({ @Size(min=5, message="The field must be at least {min} characters"), @Size(max=100, message="The field must be less than {max} characters")    })    private String username;    @Basic    @Column(name = "enabled", columnDefinition = "BIT", length = 1)    private boolean enabled;    @@JsonManagedReference    @oneToMany(cascade = {CascadeType.ALL,CascadeType.PERSIST,CascadeType.MERGE}, mappedBy = "user")    @Column(nullable = false)    private List<Authority> authorities = new ArrayList<>();}

Authority.java

@Data@Entity@Table(name ="authorities")public class Authority {    @Id    @GeneratedValue(strategy = GenerationType.AUTO)    private Long id;    @Version    private Integer version;    @NotNull    @Size.List ({ @Size(min=5, message="The field must be at least {min} characters"), @Size(max=100, message="The field must be less than {max} characters")    })    private String authority;    @JsonBackReference    @ManyToOne(fetch = FetchType.EAGER)    @JoinColumn(name = "username", nullable=false)    private User user;}

还有一点,如果我们使用

Set<>
而不是
List<>
来保存子对象,则应按此处所述正确更改equals和哈希码方法-使用java.util.Set时的Jackson错误(或功能!?)-mySet.size()始终为1



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5019728.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存