当前位置: 首页 > 知识库问答 >
问题:

org.hibernate.PersistentObjectException:传递给Persisted的分离实体:多对一单向

师冥夜
2023-03-14

我在spring boot尝试用hibernate进行多对一的单向映射。我有以下几个学生班--很多

package jpa.many.to.one.unidirectional.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
 
@Entity
@Table(name = "STUDENT")
public class Student {
 
    @Id
    @GeneratedValue
    @Column(name = "STUDENT_ID")
    private long id;
 
    @Column(name = "FIRST_NAME")
    private String firstName;
 
    @Column(name = "LAST_NAME")
    private String lastName;
 
    @Column(name = "SECTION")
    private String section;
 
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="UNIVERSITY_ID")
    private University university;
 
 
    public Student() {
    }
 
    public Student(String firstName, String lastName, String section, University university) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.section = section;
        this.university = university;
    }
 
    public long getId() {
        return id;
    }
 
    public void setId(long id) {
        this.id = id;
    }
 
    public String getFirstName() {
        return firstName;
    }
 
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
 
    public String getLastName() {
        return lastName;
    }
 
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
 
    public String getSection() {
        return section;
    }
 
    public void setSection(String section) {
        this.section = section;
    }
 
    public University getUniversity() {
        return university;
    }
 
    public void setUniversity(University university) {
        this.university = university;
    }
 
    @Override
    public String toString() {
        return "Student [id=" + id + ", firstName=" + firstName + ", lastName="
                + lastName + ", section=" + section + "]";
    }
 
}

大学-一个班级

package jpa.many.to.one.unidirectional.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
 
@Entity
@Table(name = "UNIVERSITY")
public class University {
 
    @Id
    @GeneratedValue
    @Column(name = "UNIVERSITY_ID")
    private long id;
 
    @Column(name = "NAME")
    private String name;
 
    @Column(name = "COUNTRY")
    private String country;
 
    public University() {
 
    }
 
    public University(String name, String country) {
        this.name = name;
        this.country = country;
    }
 
    public long getId() {
        return id;
    }
 
    public void setId(long id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getCountry() {
        return country;
    }
 
    public void setCountry(String country) {
        this.country = country;
    }
 
    @Override
    public String toString() {
        return "University [id=" + id + ", name=" + name + ", country=" + country + "]";
    }
 
}

应用程序另存和运行为

package jpa.many.to.one.unidirectional;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import jpa.many.to.one.unidirectional.model.Student;
import jpa.many.to.one.unidirectional.model.University;
import jpa.many.to.one.unidirectional.repo.StudentRepo;
import jpa.many.to.one.unidirectional.repo.UniversityRepo;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
//      SpringApplication.run(Application.class, args);

        ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(Application.class, args);
        UniversityRepo universityRepo = configurableApplicationContext.getBean(UniversityRepo.class);
        StudentRepo studentRepo = configurableApplicationContext.getBean(StudentRepo.class);

        University university = new University("MG", "India");
        
        Student fstudent = new Student("Deeksha", "Sivakumar", "A", university);
        fstudent.setUniversity(university);
        studentRepo.save(fstudent);
        
        
        Student fstudent1 = new Student("Sivakumar", "Nair", "A", university);
        fstudent1.setUniversity(university);
        studentRepo.save(fstudent1);

    }

}

但我的原因是:org.hibernate.PersistentObjectException:传递给Persisted的分离实体:jpa.many.to.oniredirectoral.model.university(位于org.hibernate.event.internal.defaultPersistentListener.onPersist(DefaultPersistEventListener.java:120)(位于org.hibernate.event.service.internal.eventListener.java:113)(位于t org.hibernate.event.internal.AbstractSaveEventListener.PerformSaveorReplicate(AbstractSaveEventListener.java:264)在org.hibernate.event.internal.AbstractSaveEventListener.PerformSave(AbstractSaveEventListener.java:193)在org.hibernate.event.internal.AbstractSaveEventListener.SaveWithGeneratedId(AbstractSaveEventListener.135)在方法)在java.base/jdk.internal.reflect.NativeMethodAccessorImpl.Invoke(NativeMethodAccessorImpl.java:62)在java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.Invoke(DelegatingMethodAccessorImpl.java:43)在java.base/java.lang.Reflect.Method.Invoke(Method.java:567)在OnMetadata.Invoke(ImplementationInvocationMetadata.java:72)位于org.springframework.data.repository.core.support.repositorycomposition$RepositoryFragments.Invoke(Repositorycompositor.java:382)位于org.springframework.data.repository.core.support.repositorycomposition.Invoke(Repositorycompositor.java:205)位于InvokingMethodInceptor.Invoke(DefaultMethodInceptor.java:80)在org.springFramework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)在org.springFramework.transaction.Inceptor.TransactionAspectSupport.InvokeWithinTransaction(TransactionAspectSupport.367)在

我在多对一注释中尝试了cascade.merge和optional-=false。但我得到了上面提到的错误,请帮助

共有1个答案

公良天逸
2023-03-14

@ManyToOne(cascade=CascadeType.All)在您的情况下,这似乎是一个坏主意,因为删除一个学生将导致删除相关的大学。由于一所大学可以有多个学生,另一个学生将成为孤儿

我认为你应该在大学实体中使用这个关系,而不是学生实体

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="STUDENT_ID")
private List<Student> students;

如果您想使用您的模型,请尝试在将大学设置为student之前保存它(因为在您的代码中,您试图保存同一个大学两次,第一次是fstudent,第二次是fstudent1),在这种情况下,我建议您不要使用级联:

  University university = universityRepo.save(new University("MG", "India"));
    
    Student fstudent = new Student("Deeksha", "Sivakumar", "A", university);
    fstudent.setUniversity(university);
    studentRepo.save(fstudent);
    
    
    Student fstudent1 = new Student("Sivakumar", "Nair", "A", university);
    fstudent1.setUniversity(university);
    studentRepo.save(fstudent1);
 类似资料: