Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

eclipse - SpringBoot @OneToMany infinite loop with Lombok

My project using SpringBoot, it has bidirectional mapping @OneToMany

@Entity
@Table(name = "T_S")
@Getter
@Setter
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@ToString
    public class S implements Serializable {
        @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
        @JoinColumn(name = "something", referencedColumnName = "A_ID", nullable = false, insertable = false, updatable = false)
        @OptimisticLock(excluded = false)
        private Set<A> act = new HashSet<>();
        ....something...
    }

    @Entity
    @Table(name = "T_A")
    @Getter
    @Setter
    @EqualsAndHashCode
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public class A{
        @ManyToOne(optional = true, fetch = FetchType.LAZY)
        @JoinColumn(name = "A_ID", insertable = false, updatable = false)
        private S sub;
    }
  

whe i get data from repository it make infinitive loop. How to fix it ?

question from:https://stackoverflow.com/questions/65930344/springboot-onetomany-infinite-loop-with-lombok

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have fixed this issue by override three method hashCode ,toString, equals in entity A

@Override
public int hashCode() {
    //something
}

@Override
public String toString() {
    //something
}

@Override
public boolean equals(Object obj) {
    //something
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...