【问题标题】:Infinite recursion (Stackoverflow) with JPA and Biderectional ManyToMany Relantionship具有 JPA 和双向多对多关系的无限递归(堆栈溢出)
【发布时间】:2016-12-21 15:52:50
【问题描述】:

我有一个Spring Boot 1.3.5-RELEASE 应用程序,它使用JPA 将我的USERSROLES 关联到Bi-directional ManyToMany 关系。

用户

@Table(name = "Users")
@Entity
public class User extends BaseEntity {

    @NotEmpty
    @Column(unique = true)
    private String username;

    @NotEmpty
    private String password;

    @JoinColumn(name = "user_iid")
    @OneToMany  
    private Set<UserRole> userRoles;

    //getters and setters

UserRole(中间表)

@Table(uniqueConstraints = @UniqueConstraint(columnNames = { "user_iid", "role_iid" }))
@Entity
public class UserRole extends BaseEntity {

    @RestResource(exported = false)
    @ManyToOne
    @NotNull    
    private User user;

    @ManyToOne
    private Role role;  

    //getters and setters

角色

@Entity
public class Role extends BaseEntity {

    @NotEmpty
    @Column(unique = true)
    private String name;

    @JoinColumn(name = "role_iid")
    @OneToMany
    private Set<UserRole> userRoles;

    //getters and setters

BaseEntity 是一个具有IdsVersion 生成器的类。

存储库

@Repository
public interface Repository extends JpaRepository<Role, String> {

    Role findByIid(@Param("iid") final String iid);

当我卷曲 localhost:8080/roles/search/findByIid?iid=1 时,我得到一个 StackOverflow。如果对象不存在,应用程序响应正常。

我已经尝试过@JsonIgnore,但还是不行。

谢谢

【问题讨论】:

    标签: jpa spring-boot many-to-many stack-overflow infinite-recursion


    【解决方案1】:

    我得到了答案。

    我更新了Spring Boot to 1.4.2-RELEASE(这是最后一个),一切都很顺利。我认为随着更新,它会更新 JPA 和 Hibernate,并使它们能够更好地处理多对多关系。

    【讨论】:

      猜你喜欢
      • 2017-09-14
      • 2020-06-19
      • 2020-07-25
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-04
      相关资源
      最近更新 更多