当前位置: 首页 > news >正文

JPA多對多關係時 JSON 序列化解决方案

前言

在 JPA 中處理 多對多 (Many-to-Many) 關係,不使用 @ManyToMany 註解方式,而是將這個關係拆解為兩個一對多的單向關係,並為中間表創建一個獨立的Entity.

代碼如下:

@Entity @Data @NoArgsConstructor @AllArgsConstructor @Builder @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "username", nullable = false) private String username; @Column(name = "password", nullable = false) private String password; @Column(name = "first_name", nullable = false) private String firstName; @Column(name = "last_name") private String lastName; @Column(name = "email", nullable = false, unique = true) private String email; @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) private Set<UserRole> roles = new HashSet<>(); public User(String username, String password, String firstName, String lastName, String email) { this.username = username; this.password = password; this.firstName = firstName; this.lastName = lastName; this.email = email; } }
@Entity @Data @NoArgsConstructor @AllArgsConstructor @Builder @Table(name = "roles") public class Role { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(length = 20) private String name; public Role(String name) { this.name = name; } @OneToMany(mappedBy = "role", cascade = CascadeType.ALL, orphanRemoval = true) private Set<UserRole> userRoles = new HashSet<>(); }
@Data @Embeddable public class UserRoleId implements Serializable { // 與 UserRole.java 中 @MapsId 的名稱一致 @Column(name = "user_id") private Long userId; @Column(name = "role_id") private Long roleId; public UserRoleId() { } public UserRoleId(Long userId, Long roleId) { this.userId = userId; this.roleId = roleId; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; UserRoleId that = (UserRoleId) o; return Objects.equals(userId, that.userId) && Objects.equals(roleId, that.roleId); } @Override public int hashCode() { return Objects.hash(userId, roleId); } }
@Entity @Data @NoArgsConstructor @AllArgsConstructor @Builder @Table(name = "users_roles") public class UserRole implements Serializable { // ID @EmbeddedId private UserRoleId id; // 關係到 User, userId 對映到 UserRoleId 中的 userId @ManyToOne(fetch = FetchType.LAZY) @MapsId("userId") @JoinColumn(name = "user_id") private User user; // 關係到 Role @ManyToOne(fetch = FetchType.LAZY) @MapsId("roleId") @JoinColumn(name = "role_id") private Role role; @Column(name = "assigned_at") private LocalDateTime assignedAt; }

當我們 序列化 User 實例時,Jackson 會拋出JsonMappingException異常

顯示Exception如下:

原因:

這個錯誤發生在 Jackson 嘗試將您的 JPA 實體 User 序列化為 JSON 字串時,Jackson 序列化器仍然發現了一個循環

無限遞歸序列化錯誤原因:

循環序列化的多對多結構 User <-> UserRole <-> Role

  1. Jackson 序列化 User。
  2. 在序列化 User 的屬性時,遇到 roles 集合 (Set<UserRole>)。
  3. 序列化 UserRole 時,遇到 User 實體 (@ManyToOne private User user;)。
  4. Jackson 再次嘗試序列化這個 User 物件,回到步驟 1,形成無限循環。

註: Jackson 預設的最大遞歸深度是 1000 層,當達到這個限制時,它會拋出這個錯誤以避免堆棧溢出(StackOverflowError)。

任務

針對 User 序列化為 JSON 字串時,Jackson JSON 的無限遞迴問題,提出處理雙向關係的方法

處理動作

步驟一. 首先建立一個測試案例測試:

@Transactional @SpringBootTest public class UserRoleRelationshipTest { @Test void testReadUserRoleRelationship() { try { List<User> users = userRepository.findAll(); // 獲取所有用戶 System.out.println("****** 獲取所有用戶: ******"); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(new JavaTimeModule()); String jsonArray = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(users); System.out.println(jsonArray); } catch (JsonProcessingException e) { e.printStackTrace(); } }

步驟二. 預備測試資料,已存 DB

測試用

Table users

步驟三. 實作方案

方案一:使用@JsonIgnore

不想序列化某個屬性,使用@JsonIgnore註解來忽略關係中的某個屬性

選項1

public class User { . . . @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) @JsonIgnore private Set<UserRole> roles = new HashSet<>(); . . . }

測試結果

% mvn test

選項2

public class UserRole implements Serializable { . . . @ManyToOne(fetch = FetchType.LAZY) @MapsId("userId") // 映射到 UserRoleId 中的 userId @JoinColumn(name = "user_id") @JsonIgnore private User user; @ManyToOne(fetch = FetchType.LAZY) @MapsId("roleId") // 映射到 UserRoleId 中的 roleId @JoinColumn(name = "role_id") @JsonIgnore private Role role; . . . }

測試結果

% mvn test

方案二:使用@JsonManagedReferences@JsonBackReferences

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) @JsonManagedReference private Set<UserRole> roles = new HashSet<>(); @OneToMany(mappedBy = "role", cascade = CascadeType.ALL, orphanRemoval = true) @JsonManagedReference private Set<UserRole> userRoles = new HashSet<>(); @ManyToOne(fetch = FetchType.LAZY) @MapsId("userId") // 映射到 UserRoleId 中的 userId 屬性 @JoinColumn(name = "user_id") @JsonBackReference private User user; @ManyToOne(fetch = FetchType.LAZY) @MapsId("roleId") // 映射到 UserRoleId 中的 roleId 屬性 @JoinColumn(name = "role_id") @JsonBackReference private Role role;

執行測試結果:

http://www.jsqmd.com/news/79075/

相关文章:

  • 高效配置yowsup项目的pre-commit代码质量检查实战指南
  • 中文论文格式模板下载指南:让学术写作更轻松 ✨
  • 大作业笔记-4
  • Blade构建系统终极指南:新手快速上手指南
  • iOS评论系统深度解析:构建高性能实时交互体验的完整指南
  • 智能文档处理系统快速上手指南
  • 重构Kubernetes工作流:Lens插件架构的5个生产力提升场景
  • Ursa.Avalonia样式系统深度解析:模块化设计与主题切换完整指南
  • YubiKey SSH认证终极指南:从零开始实现无密码安全登录
  • 2025.12.12博客
  • 29、Linux 软件使用与故障排除指南
  • Path of Exile 2终极物品过滤器配置指南
  • Extreme Programming--front-end and back-end separation contacts programming
  • 终于交出焚诀了,运营新思路:短视频动漫化
  • 认证--JSON
  • 30、Linux使用技巧与常见命令全解析
  • 不缺席娃成长,也能过法考!宝妈备战法考秘籍,UU带你碎片化时间稳过线
  • Excel VBA快速入门:7天从零到精通终极指南
  • 【Anthropic分享博客】Anthropic 内部的 Agentic Workflow 工程实践
  • 孤能子视角:“DeepSeek偏向中文思考“本质推测
  • 31、Linux DVD-ROM 使用指南与相关知识详解
  • RaceControl终极指南:告别F1TV官方客户端的所有痛点
  • 基于python大数据的的海洋气象数据可视化平台 - 详解
  • 算法系列(Algorithm)- 快速排序
  • AutoHotkey鼠标轨迹自动化终极指南:从零开始实现精准操作回放
  • RobotStudio2025全功能授权
  • 授权vvvvvv
  • 如何快速搭建自动驾驶平台:开源汽车控制系统的完整指南
  • dotNetFx40_Full_x86_x64完整安装包:快速部署.NET Framework 4.0开发环境
  • MCP安全认证终极指南:如何在7天内从零到部署的完整实战