[英]“Save the transient instance before flushing ” error
我有2个对象,即Discipline和Category对象。 这两个使用MANY-TO-ONE关系连接。 我正在尝试使用此JSON对象通过Postman创建一个新的Discipline:
{
"name": "test_sample"
}
POJOs:
学科:
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对象而不先设置这两者之间的关系。
您应该添加@ManyToOne注释: cascade=CascadeType.ALL
。 您遇到此错误的原因是您要保存数据库中不存在的子对象,而hibernate不允许这样的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.