gpt4 book ai didi

java - 当我尝试进行 JPQL JOIN 查询时发生 StackOverflowError

转载 作者:行者123 更新时间:2023-12-02 10:47:00 37 4
gpt4 key购买 nike

我有以下实体:

@Data
@Entity
@Table(name = "artist")
public class Artist {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="artist_id")
private int id;
@Column(name="artist_name", length = 2000)
private String name;
private String country;
@OneToMany(mappedBy = "artist", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Song> songs = new ArrayList<>();

public Artist(String name, String country){
this.name=name;
this.country=country;
}
}

@Data
@Entity
@Table(name = "songs")
public class Song {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int songId;
@Column(name = "text", length = 65600)
private String text;
private Double rating;
private String songName;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "artist_id", nullable = false)
public Artist artist;

public Song(String text, Double rating, String songName, Artist artist) {
this.text = text;
this.rating = rating;
this.songName = songName;
this.artist = artist;
}
}

我尝试按艺术家姓名模式获取所有歌曲:

@Query("SELECT s FROM Song s WHERE s.artist.name LIKE CONCAT('%',:name,'%')")
列表 findByArtistsName(@Param("name") String name);

以及所有艺术家的歌曲名称模式:

@Query("SELECT a FROM Artist a JOIN FETCH a.songs s WHERE s.songName LIKE CONCAT('%',:pattern,'%')")
List<Artist> findBySong(@Param("pattern") String songName);

为此,我分别使用 JPQL 查询。但两者都给我同样的感觉堆栈溢出错误。

> 
>
> Caused by: java.lang.StackOverflowError: null
> at sun.misc.FloatingDecimal$BinaryToASCIIBuffer.dtoa(FloatingDecimal.java:431)
> ~[na:1.8.0_151]
> at sun.misc.FloatingDecimal$BinaryToASCIIBuffer.access$100(FloatingDecimal.java:259)
> ~[na:1.8.0_151]
> at sun.misc.FloatingDecimal.getBinaryToASCIIConverter(FloatingDecimal.java:1785)
> ~[na:1.8.0_151]
> at sun.misc.FloatingDecimal.getBinaryToASCIIConverter(FloatingDecimal.java:1738)
> ~[na:1.8.0_151]
> at sun.misc.FloatingDecimal.toJavaFormatString(FloatingDecimal.java:70)
> ~[na:1.8.0_151]
> at java.lang.Double.toString(Double.java:204) ~[na:1.8.0_151]
> at java.lang.Double.toString(Double.java:644) ~[na:1.8.0_151]
> at java.lang.String.valueOf(String.java:2994) ~[na:1.8.0_151]
> at java.lang.StringBuilder.append(StringBuilder.java:131) ~[na:1.8.0_151]
> at com.music.entity.Song.toString(Song.java:8) ~[classes/:na]
> at java.lang.String.valueOf(String.java:2994) ~[na:1.8.0_151]
> at java.lang.StringBuilder.append(StringBuilder.java:131) ~[na:1.8.0_151]
> at java.util.AbstractCollection.toString(AbstractCollection.java:462)
> ~[na:1.8.0_151]
> at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:510)
> ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
> at java.lang.String.valueOf(String.java:2994) ~[na:1.8.0_151]
> at java.lang.StringBuilder.append(StringBuilder.java:131) ~[na:1.8.0_151]
> at com.music.entity.Artist.toString(Artist.java:9) ~[classes/:na]
> at java.lang.String.valueOf(String.java:2994) ~[na:1.8.0_151]
> at java.lang.StringBuilder.append(StringBuilder.java:131) ~[na:1.8.0_151]
> at com.music.entity.Song.toString(Song.java:8) ~[classes/:na]
> at java.lang.String.valueOf(String.java:2994) ~[na:1.8.0_151]
> at java.lang.StringBuilder.append(StringBuilder.java:131) ~[na:1.8.0_151]
> at java.util.AbstractCollection.toString(AbstractCollection.java:462)
> ~[na:1.8.0_151]

解决这个问题的正确方法是什么?

最佳答案

这几乎肯定是由您使用 Lombok 引起的以及 @Data 注释,它是其他 Lombok 注释的快捷方式:

@ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor

@ToString 注释会自动生成一个 toString 方法,其中包含所有字段。堆栈跟踪的底部指向集合上的 toString 方法调用,如果您向上跟踪堆栈,您可以发现导致堆栈溢出的循环:

AbstractCollection.toString -> Song.toString -> Artist.toString -> AbstractCollection.toString -> Song.toString ...

您的Artist实体拥有Song实体的集合,其中包含对Artist实体的引用等等。您可以看到它在调用 toString 方法时如何陷入循环并最终耗尽堆栈帧/达到限制。

要么删除 @Data 注释并仅应用一个子集,要么定义您自己的 toString 方法来避免循环。

关于java - 当我尝试进行 JPQL JOIN 查询时发生 StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52471218/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com