Java 错误:org.hibernate.PersistentObjectException:已分离的实体传递给persist

Java 错误:org.hibernate.PersistentObjectException:已分离的实体传递给persist,java,mysql,reactjs,spring-boot,Java,Mysql,Reactjs,Spring Boot,我正在开发SpringBoot+ReactJs应用程序。基本上我有两个实体(品牌和家族),它们有一对多的关系 @Entity @Table(name = "brands") public class Brand extends UserDateAudit { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @NotBlank @Size(max = 140

我正在开发SpringBoot+ReactJs应用程序。基本上我有两个实体(品牌和家族),它们有一对多的关系

@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;
当我创建“Family”对象时,它抛出以下错误

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);
        }
通过调试,我发现“familyRequest”既有“name”又有“Brand\u id”

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!'
                });
            }
        });
    }
我不确定我为品牌id设置的值是否正确。
请帮我解决这个问题。

您的映射不正确。的一侧必须使用mappedBy属性


将所有操作级联到多个One上是没有意义的:当您创建一个族时,您不想创建它的品牌:它已经存在了。删除族时,您不想删除其品牌,因为它被许多其他族引用。

您的映射不正确。的一侧必须使用mappedBy属性

将所有操作级联到多个One上是没有意义的:当您创建一个族时,您不想创建它的品牌:它已经存在了。删除一个族时,您不想删除其品牌,因为它被许多其他族引用

 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!'
                });
            }
        });
    }