TransientObjectException:对象引用了一个未保存的瞬态实例-在刷新之前保存该瞬态实例Error

时间:2019-02-21 00:53:51

标签: java spring-boot jpa

我陷入了这个错误。

这是我要实现的数据库架构。

enter image description here

所以我为CRUD操作创建了History,HistoryAuth,HistoryAuthType,HistoryTag和HistoryRepository。

History.java

package com.wrapsody.demo;

import lombok.*;

import javax.persistence.*; import java.util.ArrayList; import java.util.List;

@NoArgsConstructor(access = AccessLevel.PROTECTED) @Data @Entity public class History {
    @Id
    @GeneratedValue
    private Long historyId;

    @Column
    private String historyMasterName;

    @Column
    private String historyFreeSetName;

    @OneToMany
    @JoinColumn
    private List<HistoryTag> tags = new ArrayList<HistoryTag>();

    @OneToMany
    @JoinColumn
    private List<HistoryAuth> auths = new ArrayList<HistoryAuth>();

    @Builder
    History(String historyMasterName, String historyFreeSetName) {
        this.historyMasterName = historyMasterName;
        this.historyFreeSetName = historyFreeSetName;
    } }

HistoryAuth.java

package com.wrapsody.demo;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
public class HistoryAuth {
    @Id
    @GeneratedValue
    private Long historyAuthId;

    @Column
    private  String historyAuthName;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(insertable = false, updatable = false)
    private HistoryAuthType historyAuthType;

    @Builder
    HistoryAuth(String historyAuthName, HistoryAuthType historyAuthType) {
        this.historyAuthName = historyAuthName;
        this.historyAuthType = historyAuthType;
    }
}

HistoryAuthType.java

package com.wrapsody.demo;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
public class HistoryAuthType {
    @Id
    @GeneratedValue
    private  Long historyAuthTypeId;

    @Column
    private String historyAuthTypeName;

    @Builder
    HistoryAuthType(String historyAuthTypeName) {
        this.historyAuthTypeName = historyAuthTypeName;
    }
}

HistoryTag.java

package com.wrapsody.demo;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
public class HistoryTag {
    @Id
    @GeneratedValue
    private Long historyTagId;

    @Column
    private String historyTagName;

    @Builder
    HistoryTag(String historyTagName) {
        this.historyTagName = historyTagName;
    }
}

我想知道我是否实施得很好?

Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.wrapsody.demo.HistoryAuth

为什么会给我这个错误?

0 个答案:

没有答案