خرید بک لینک

Vote count: 0

i mean without any n+1 queries. 3 - it's normal

i have an entites:

@Entity
@Table(name = "tags")
public class Tag {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Column(name = "title")
    private String title;
}

@Entity
@Table(name = "stations")
public class Station {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Column(name = "title")
    private String title;
}

@Entity
@Table(name = "songs")
public class Song {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Column(name = "title")
    private String title;

    @ManyToMany
    @JoinTable(
            name = "songs_stations",
            joinColumns = {
                    @JoinColumn(
                            name = "song_id",
                            referencedColumnName = "id"
                    )
            },
            inverseJoinColumns = {
                    @JoinColumn(
                            name = "station_id",
                            referencedColumnName = "id"
                    )
            }
    )
    private List<Station> stations;

    @ManyToMany
    @JoinTable(
            name = "songs_tags",
            joinColumns = {
                    @JoinColumn(
                            name = "song_id",
                            referencedColumnName = "id"
                    )
            },
            inverseJoinColumns = {
                    @JoinColumn(
                            name = "tag_id",
                            referencedColumnName = "id"
                    )
            }
    )
    private List<Tag> tags;
}

and a repository:

public interface SongRepository extends CrudRepository<Song, Long> {

    @Query("SELECT s FROM Song s LEFT JOIN FETCH s.tags LEFT JOIN FETCH s.stations")
    public List<Song> completeFindAllSongs();
}

  • so, i can't use eager loading in completeFindAllSongs() cause of caot simultaneously fetch multiple bags
  • can't use @NamedEntityGraph
  • and i can't load data manually, cause i don't have access to songs_tags table
  • please don't advise to use @LazyCollection

what should i do?

asked 8 mins ago

1 Answer

Vote count: 0

so, i can't use eager loading in completeFindAllSongs() cause of caot simultaneously fetch multiple bags

This error should go away if you change your entities to use "Set" instead of "List" for OneToMany and ManyToMany relations.

answered 4 mins ago

برچسب: نویسنده: استخدام کار تاريخ: چهارشنبه 1 ارديبهشت 1395 ساعت: 23:08

صفحه بندی