“刷新前保存瞬态实例”错误

时间:2019-06-06 21:57:08

标签: hibernate spring-boot jpa

我有2个对象,学科和类别对象。这两个通过MANY-TO-ONE关系进行连接。我正在尝试使用以下JSON对象通过邮递员创建新的纪律:

{
    "name": "test_sample"
}

POJO:

学科:

import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;

@Entity(name = "discipline")
@Where(clause = "is_deleted=0")
@SQLDelete(sql = "UPDATE discipline SET is_deleted = 1 WHERE id = ?")
public class Discipline extends BaseEntity{
    @Column(name = "name")
    private String name;
    @ManyToOne
    @JoinColumn(name = "category_id")
    private Category category;


    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

}

类别:

import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;

@Entity(name = "category")
@Where(clause = "is_deleted=0")
@SQLDelete(sql = "UPDATE category SET is_deleted = 1 WHERE id = ?")
public class Category extends BaseEntity{
    @Column(name = "name")
    private String name;

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

}

我收到此错误:

{
    "timestamp": "2019-06-06T21:22:42.746+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.test.meeting.meeting.data.model.Discipline.category -> com.test.meeting.meeting.data.model.Category; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.test.meeting.meeting.data.model.Discipline.category -> com.test.meeting.meeting.data.model.Category",
    "path": "/api/v1/discipline"
}

如何在不先设置两者之间的关系的情况下保存Discpline对象。

1 个答案:

答案 0 :(得分:0)

您应该添加@ManyToOne批注:signature = cipher.get_signature(js, stream['s']) 。出现此错误的原因是您要保存数据库中不存在的子对象,并且休眠状态不允许这样的操作。