我正在研究 Spring-boot+ ReactJs 应用程序。基本上我有 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;
和
@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;
当我创建“家庭”对象时,它会引发以下错误。
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() 如下。
public Family createFamily(@Valid FamilyRequest familyRequest) {
Family family = new Family();
family.setName(familyRequest.getName());
family.setBrand(familyRequest.getBrand());
return familyRepository.save(family);
}
我不确定我为brand_id 设置的值是否正确。请帮我解决这个问题。
森栏
相关分类