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

时间:2018-09-23 23:43:04

标签: java join spring-data-jpa stack-overflow jpql

我有以下实体:

@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(“从歌曲的WHERE s.artist.name中选择SELECT CONCAT('%',: name,'%')”)
列出findByArtistsName(@Param(“ 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查询。但是两个让我一样 StackOverflowError。

> 
> 
>     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]

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

1 个答案:

答案 0 :(得分:1)

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

  

@ ToString,@ EqualsAndHashCode,所有字段上的@Getter和所有非最终字段上的@Setter,以及@RequiredArgsConstructor

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

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

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

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