[英]Error: org.hibernate.PersistentObjectException: detached entity passed to persist
I'm working on Spring-boot+ ReactJs application. 我正在开发Spring-boot + ReactJs应用程序。 BAsically I have 2 Entities ( Brand and Family) which have One to Many Relationship. 我基本上有2个实体(品牌和家庭),它们之间存在一对多的关系。
@Entity
@Table(name = "brands")
public class Brand extends UserDateAudit {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@Size(max = 140)
private String name;
@Lob @JsonProperty("image")
private byte[] image;
@OneToMany
private Set<Family> family;
and 和
@Entity
@Table(name = "family")
public class Family extends UserDateAudit {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@Size(max = 140)
private String name;
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToOne(fetch = FetchType.LAZY,cascade=CascadeType.ALL, optional = false)
@JoinColumn(name = "brand_id")
private Brand brand;
When I creating 'Family' object it throws following error. 当我创建“家庭”对象时,它引发以下错误。
org.hibernate.PersistentObjectException: detached entity passed to persist: com.example.polls.model.Brand
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:124) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:807) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:774) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
createFamily() is as follows. createFamily()如下。
public Family createFamily(@Valid FamilyRequest familyRequest) {
Family family = new Family();
family.setName(familyRequest.getName());
family.setBrand(familyRequest.getBrand());
return familyRepository.save(family);
}
Through debuging I found the 'familyRequest' have both 'name' and 'Brand_id' 通过调试,我发现“ familyRequest”同时具有“ name”和“ Brand_id”
ReactJS: ReactJS:
handleSubmit(event) {
event.preventDefault();
// console.log('image: '+this.state.brand_id.text);
const familyData = {
name: this.state.name.text,
brand: {id : this.state.brand_id.text}
// band_id: this.state.band_id
};
createFamily(familyData)
.then(response => {
this.props.history.push("/");
}).catch(error => {
if(error.status === 401) {
this.props.handleLogout('/login', 'error', 'You have been logged out. Please login create family.');
} else {
notification.error({
message: 'Polling App',
description: error.message || 'Sorry! Something went wrong. Please try again!'
});
}
});
}
I'm not sure whether I set value for brand_id is correct. 我不确定我为brand_id设置的值是否正确。 Please help me to solve this isssue. 请帮助我解决这个问题。
Your mapping is incorrect. 您的映射不正确。 The one side of a bidirectional OneToMany must use the mappedBy attribute. 双向OneToMany的一侧必须使用mappingBy属性。
And cascading all operations on a ManyToOne makes no sense: when you create a family, you don't want to create its brand: it already exists. 将所有操作层叠在ManyToOne上是没有意义的:创建家族时,您不想创建品牌:它已经存在。 And when you delete a family, you don't want to delete its brand, since it's referenced by many other families. 而且,当您删除一个家庭时,您不想删除其品牌,因为许多其他家庭都引用了该品牌。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.