PersistenceUnit:默认。无法构建 Hibernate SessionFactory - 当我在 1 个类中使用许多关系时出错

问题描述 投票:0回答:0

我有 1 个名为 Dashboard 的类,我在 userId 字段中使用 @OneToOne,它可以正常工作,如下所示:

@Entity
@Data
public class Dashboard {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToOne
    @JoinColumn(name = "user_id", referencedColumnName = "id")
    private User userId;

在那之后,我添加了另一个关系@OneToMany 来指示仪表板可以有很多小部件。我使用 ArrayList 来存储它,如下所示:

@Entity
@Data
public class Dashboard {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToOne
    @JoinColumn(name = "user_id", referencedColumnName = "id")
    private User userId;
    
    @OneToMany(fetch = FetchType.EAGER)
    private Collection<Widget> widgets = new ArrayList<>();

出现错误,我不知道如何解决,我尝试在互联网上搜索,但没有任何帮助。 代码仅适用于 1 个关系(@OneToOne 或 @OneToMany),但当我尝试同时使用两者时它不起作用。

错误:引起:javax.persistence.PersistenceException: [PersistenceUnit: default] 无法构建 Hibernate SessionFactory; 嵌套异常是 org.hibernate.loader.MultipleBagFetchException: 不能同时取多个包: [com.project.myproject.auth.model.User.roles, com.project.myproject.dashboard.maindashboard.Dashboard.widgets]

java spring-boot hibernate spring-data-jpa foreign-keys
© www.soinside.com 2019 - 2024. All rights reserved.