簡體   English   中英

錯誤:org.hibernate.PersistentObjectException:分離的實體傳遞給持久化

[英]Error: org.hibernate.PersistentObjectException: detached entity passed to persist

我正在開發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);
        }

通過調試,我發現“ familyRequest”同時具有“ name”和“ Brand_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!'
                });
            }
        });
    }

我不確定我為brand_id設置的值是否正確。 請幫助我解決這個問題。

您的映射不正確。 雙向OneToMany的一側必須使用mappingBy屬性。

將所有操作層疊在ManyToOne上是沒有意義的:創建家族時,您不想創建品牌:它已經存在。 而且,當您刪除一個家庭時,您不想刪除其品牌,因為許多其他家庭都引用了該品牌。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM