Infinite recursion (Stackoverflow) with JPA and Biderectional ManyToMany Relantionship

210 views Asked by At

I have a Spring Boot 1.3.5-RELEASE application which is using JPAto Relate my USERS to the ROLES with a Bi-directional ManyToMany relationship.

User

@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 (intermediary table)

@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

Role

@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 is a class with Ids and Version generator.

Repository

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

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

When I cURL a localhost:8080/roles/search/findByIid?iid=1 I get a StackOverflow. If the object does not exist, the application respond fine.

I already tried @JsonIgnore but does not work.

Thanks

1

There are 1 answers

0
José Mendes On BEST ANSWER

I got the answer.

I updated the Spring Boot to 1.4.2-RELEASE (which is the last) and everything worked like a charm. I think with the update it updates JPA and Hibernate and make them handle better those ManyToMany relantionships.