具有JPA和Biderectional ManyToMany Relantionship的无限递归(Stackoverflow)

时间:2016-12-21 15:52:51

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

我有一个Spring Boot 1.3.5-RELEASE个应用程序正在使用JPAUSERSROLES关联起来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,但无效。

由于

1 个答案:

答案 0 :(得分:0)

我得到了答案。

我更新了Spring Boot to 1.4.2-RELEASE(这是最后一次),一切都像魅力一样。我认为通过更新它可以更新JPA和Hibernate并使它们更好地处理那些ManyToMany关系。