【问题标题】:Spring boot entities,relationships and repository confusionSpring Boot 实体、关系和存储库混淆
【发布时间】:2019-03-09 12:10:16
【问题描述】:

我对关系在实体中的工作方式以及这对我的 JPA 存储库意味着什么感到困惑。

我有一个名为 Loan 的类,它存储每笔贷款的专辑列表。

我有一个借阅库和一个专辑库。当我启动应用程序时,相册存储库中充满了相册。 AlbumId 是自动生成的。

当我创建新贷款并尝试从存储库中添加专辑时,我得到一个例外:

引起:org.springframework.dao.InvalidDataAccessApiUsageException:分离的实体传递给坚持:com.library.demo.entity.Album;嵌套异常是 org.hibernate.PersistentObjectException: 分离的实体传递给持久化:com.library.demo.entity.Album

如果我创建新贷款并即时添加新专辑,那么它会按预期工作。在调试过程中,我意识到这是因为在将新专辑即时添加到外借时,albumId 为空,大概是因为它将专辑添加到存储库并在创建外借时生成新的专辑 ID。

我的相册实体如下所示:

@Entity
public class Album implements Serializable {

private static final long serialVersionUID = 0x63A6DA99AA12AAA8L;

@Column @GeneratedValue(strategy = GenerationType.AUTO) @Id private Integer albumId;
@Column (unique=true) private String barcode;
@Column private String band;
@Column private String title;
@Column private String genre;
@Column private Integer year;
@Column private String artworkFilename;
@Column private Boolean enabled;
@Column private Boolean isLoanable;
@Column private Integer numberOfCopies;

@ManyToOne
private Loan loan;

我的贷款看起来像这样:

public class Loan implements Serializable {

private static final long serialVersionUID = 0x62B6DA99AA12AAA8L;

public void setLoanId(Integer loanId) {
    this.loanId = loanId;
}

@Column @GeneratedValue(strategy = GenerationType.AUTO) @Id private Integer loanId;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Album> albums= new ArrayList<>();

@Column private Integer customerId;
@Column private Date dateLoaned;
@Column private Date dateToReturn;
@Column private Boolean expired;

我也很困惑为什么专辑必须使用 ManyToOne 注释来引用借出。专辑为什么要提到外借?

我主要习惯于关系数据库,所以也许我以错误的方式思考事情。如果我只能将新专辑添加到贷款中,那么它就违背了我想要做的事情的目的。

【问题讨论】:

    标签: spring-boot jpa


    【解决方案1】:
        Loan and Album tables have a one-to-many relationship.
        So in Album your mapping should be like below:
        @ManyToOne
        @JoinColumn(name = "loan_id")
        private Loan loan;
    Considering loan_id is the primary key of Loan.
    And in Loan your mapping should be like below:
    @OneToMany(mappedBy = "loan", cascade = CascadeType.ALL)
        private List<Album> albums;
    
    @OneToMany and @ManyToOne defines a one-to-many and many-to-one relationship between 2 entities. @JoinColumn indicates the entity is the owner of the relationship: the corresponding table has a column with a foreign key to the referenced table. mappedBy indicates the entity is the inverse of the relationship
    

    【讨论】:

    • 请看我的回答。由于格式和大小限制,我似乎无法发表后续评论。
    • loan.setAlbums(专辑);在此语句之后尝试保存贷款对象 (loanRepository.save(loan);)
    • 这是崩溃的地方:org.springframework.beans.factory.BeanCreationException:创建名为“bootLibraryApplication”的bean时出错:调用init方法失败;嵌套异常是 org.springframework.dao.InvalidDataAccessApiUsageException: 未初始化代理传递给 persist();嵌套异常是 org.hibernate.PersistentObjectException: 未初始化的代理传递给 persist()
    • 请添加model和dao类
    • 我按照本教程开始 dzone.com/articles/… bitbucket.org/ashish1991/demo/src 如果我创建技能库并尝试将现有技能添加到用户,也会遇到同样的问题。
    【解决方案2】:

    我已经按照您的描述更新了我的来源,并对启动进行了一些更改。

    如果我先创建外借,然后将其保存到外借存储库,然后添加专辑集,它就不会崩溃了。

        Loan loan = new Loan(1, new Date(), calendar.getTime(),null);
    
        loanRepository.save(loan);
    
        List<Album> albumList = AlbumImport.getAlbumList();
        albumRepository.save(albumList);
    
    
        List<Album> albums = new ArrayList<>();
        albums.add(albumList.get(1));
        albums.add(albumList.get(5));
    
        loan.setAlbums(albums);
    

    但是,当我运行 getLoan 测试时,专辑列表为空

    【讨论】:

      【解决方案3】:

      删除 cascade = CascadeType.ALL 解决了我的问题。

      【讨论】:

        猜你喜欢
        • 2014-11-27
        • 2020-04-12
        • 1970-01-01
        • 2015-02-16
        • 1970-01-01
        • 2015-11-17
        • 2021-06-05
        • 1970-01-01
        • 2021-09-07
        相关资源
        最近更新 更多