Home > Software engineering >  @ManyToOne and @OneToMany ends up in unlimited loop when retrieved through profileRepository.getByPr
@ManyToOne and @OneToMany ends up in unlimited loop when retrieved through profileRepository.getByPr

Time:01-21

Class Jobs has Many to One relationship with Profile. When I retrieve through profileRepository.getByProfileId(id) the response returns recursive data. Also if you notice Profile has Login object. I don't want to return that as well.

@Entity
@Table(name = "tbl_profile")
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class Profile {

    @Id
    @Column(name = "profile_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    long profileId;

    @NonNull
    @Column(name = "name")
    String name;

    @Column(name = "description", nullable = false)
    String description;

    @OneToOne
    @JoinColumn(name = "login_id",
                referencedColumnName = "login_id")
    Login login;

    @OneToMany(
            mappedBy = "profile"
    )
    List<Jobs> job;

Class Jobs

@Entity
@Table(name = "tbl_job")
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class Jobs {

    @Id
    @Column(name = "job_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    long jobId;

    @NonNull
    @Column(name = "job_role", nullable = false)
    String joRole;

    @Column(name = "description", nullable = false)
    String description;

    @ManyToOne
    @JoinColumn(name = "profile_id",
                referencedColumnName = "profile_id")
    Profile profile;
}

CodePudding user response:

Use @JsonIgnore to the property to ignore the output on JSON. Also according to your business logic, recheck if you need bidirectional association. You could maybe add only unidirectional association.

  •  Tags:  
  • Related