0

I cannot run my Spring Boot application because I get an error:

Unable to build Hibernate SessionFactory; nested exception is org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags: [ru.axothy.backdammon.gameservice.model.Board.towers, ru.axothy.backdammon.gameservice.model.Tower.chips]

My entities:
Board.class:

@Getter @Setter
@Entity
@Table(name = "boards")
public class Board implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "BOARD_ID")
    private int boardId;

    @OneToMany(fetch = FetchType.EAGER)
    @JsonManagedReference
    private List<Tower> towers = new ArrayList<>();
}

Tower.class:

@Entity
@Getter @Setter
@Table(name = "towers")
public class Tower implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "TOWER_ID", nullable = false)
    private int towerId;

    @OneToMany(fetch = FetchType.EAGER)
    @JsonManagedReference
    private List<Chip> chips = new ArrayList<>();

}

And chip.class:

@Getter @Setter
@Entity
@Table(name = "chips")
public class Chip implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "CHIP_ID")
    private int chipId;

}

I know that one of the solutions is to change List to Set, but I need List.

Ferich1
  • 5
  • 1
  • 4
  • Don't load collections EAGER that is an anti pattern, with multiple of those you will get a cartasian product. 1 Board * num rows Tower * num Rows Chip, which is too much data and impossible to reliably map. Just remove the EAGER. – M. Deinum Jul 11 '23 at 10:12

0 Answers0