0

i have this code

USER:

@Builder
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String username;
@Column(name = "PASSWORD")
private String password;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
private Role role;
@Column(name = "EMAIL")
private String email;
private String country;
private Status status;
@OneToMany(mappedBy = "author",fetch = FetchType.EAGER)
private List<Message> messages;
@OneToMany(mappedBy = "author",fetch = FetchType.EAGER)
private List<Comment> comments;
@OneToOne(optional = false, cascade = CascadeType.ALL)
@JoinColumn(name = "GENDER_ID")
private Gender gender;
@OneToOne(optional = false,cascade = CascadeType.ALL)
@JoinColumn(name = "ADDRESS_ID")
private Address address;
@ManyToMany
@JoinTable(
        name = "user_subscriptions",
        joinColumns =  @JoinColumn(name = "CHANNEL_ID") ,
        inverseJoinColumns =  @JoinColumn(name = "SUBSCRIBER_ID"))
private Set<User> subscribers;

@ManyToMany
@JoinTable(
        name = "user_subscriptions",
        joinColumns =  @JoinColumn(name = "SUBSCRIBER_ID") ,
        inverseJoinColumns =  @JoinColumn(name = "CHANNEL_ID"))
private Set<User> subscriptions;
}

COMMENT:

@Data
@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String header;
private String text;
private int likeCount;
@ManyToOne(optional = false, cascade = CascadeType.ALL)
@JoinColumn(name = "USER_ID")
private User author;
@ManyToOne(optional = false, cascade = CascadeType.ALL)
@JoinColumn(name = "MESSAGE_ID")
private Message message;
}

Country:

@Data
@Entity
public class Country {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@OneToOne(optional = false, mappedBy = "country")
private Address address;
@OneToMany(mappedBy = "country",fetch = FetchType.EAGER)
private List<City> cities;
@OneToMany(mappedBy = "country",fetch = FetchType.EAGER)
private List<Region> regions;
private String description;
}

REGION:

@Data
@Entity
public class Region {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@OneToOne(optional = false, mappedBy = "region")
private Address address;
@ManyToOne(optional = false,cascade = CascadeType.ALL)
@JoinColumn(name = "COUNTRY_ID")
private Country country;
}

And i have the following problems ,how to solve this?

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags: [com.project.webstation.Models.Domain.Country.regions, com.project.webstation.Models.Domain.User.comments]

stan
  • 71
  • 1
  • 7
  • can you share your property file, and your configuration file as well? – zawarudo Dec 23 '21 at 18:51
  • this is property file spring.datasource.driver-class-name=org.postgresql.Driver spring.datasource.url=jdbc:postgresql://localhost:5432/dbb spring.datasource.username=postgres spring.datasource.password=123456 spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update – stan Dec 23 '21 at 19:08

0 Answers0