简体   繁体   English

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

[英]“Save the transient instance before flushing ” error

I have 2 objects, the Discipline and the Category object. 我有2个对象,即Discipline和Category对象。 These 2 are connected using a MANY-TO-ONE relationship. 这两个使用MANY-TO-ONE关系连接。 I am trying to create a new Discipline via Postman using this JSON object: 我正在尝试使用此JSON对象通过Postman创建一个新的Discipline:

{
    "name": "test_sample"
}

The POJOs: POJOs:

Discipline: 学科:

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;
    }

}

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;
    }

}

I am receiving this error: 我收到此错误:

{
    "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"
}

How can I save the Discpline object without setting the relation between these two first. 如何保存Discpline对象而不先设置这两者之间的关系。

You should add in your @ManyToOne annotation: cascade=CascadeType.ALL . 您应该添加@ManyToOne注释: cascade=CascadeType.ALL The reason why you have this error is that you want to save a child object which is not present in the database and hibernate doesn't allow something like this. 您遇到此错误的原因是您要保存数据库中不存在的子对象,而hibernate不允许这样的事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在刷新之前保存临时实例 - save the transient instance before flushing ManyToMany关系:刷新之前保存临时实例 - ManyToMany relation: save the transient instance before flushing TransientObjectException:在刷新之前保存临时实例 - TransientObjectException: save the transient instance before flushing JPA在刷新之前保存临时实例 - JPA save the transient instance before flushing 如何解决对象引用未保存的瞬态实例的错误-在刷新之前保存瞬态实例? - How do I solve this error of object references an unsaved transient instance - save the transient instance before flushing? 域表出现错误“对象引用了未保存的瞬态实例-在刷新之前保存瞬态实例” - Domain table getting error “object references an unsaved transient instance - save the transient instance before flushing” Object 引用了未保存的瞬态实例 在刷新错误之前保存瞬态实例 - Object references an unsaved transient instance save the transient instance before flushing error object 引用了未保存的瞬态实例 - 在刷新错误之前保存瞬态实例 - object references an unsaved transient instance - save the transient instance before flushing error 对象引用未保存的瞬态实例-在刷新之前保存瞬态实例 - object references an unsaved transient instance - save the transient instance before flushing 对象引用一个未保存的瞬态实例,在刷新之前保存瞬态实例: - Object references an unsaved transient instance save the transient instance before flushing:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM