【问题标题】:Spring Data JPA ManyToOne BidirectionalSpring Data JPA 多对一双向
【发布时间】:2017-07-06 17:17:58
【问题描述】:

问题:在我尝试将学生对象添加到数据库之前,它一直有效,但表正在正确创建。 我无法进一步简化帖子。但主要是不需要大量阅读的代码,它是一个简单的spring数据存储库服务模型。我发布了这一切,因为我知道我做错了什么。问题出在 JPA 映射中。 我从这里得到了例子http://www.java2s.com/Tutorial/Java/0355__JPA/OneToManyBidirectional.htm

MDOELS

@Entity
public class Department {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String name;

    @OneToMany(mappedBy="department")
    private Collection<Student> students;

    public Department() {
    }

    public Department(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String deptName) {
        this.name = deptName;
    }

    public Collection<Student> getStudents() {
        return students;
    }

    public void setStudent(Collection<Student> students) {
        this.students = students;
    }

    public String toString() {
        return "Department id: " + getId() +
                ", name: " + getName();
    }
}



@Entity
public class Student {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String name;

    @ManyToOne (cascade=CascadeType.ALL)
    private Department department;

    public Student() {
    }

    public Student(String name, Department department) {
        this.name = name;
        this.department = department;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }



    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public String toString() {
        return "\n\nID:" + id + "\nName:" + name + "\n\n" + department;
    }
}

存储库

@Repository
public interface DepartmentRepository extends JpaRepository<Department, Integer> {
    Department findByName(String name);

}

@Repository
public interface StudentRepository extends JpaRepository<Student, Integer> {
    Student findByName(String name);
}

服务

@Service
public class StudentService {
    private final StudentRepository studentRepository;

    @Autowired
    public StudentService(StudentRepository studentRepository) {
        this.studentRepository = studentRepository;

    }

    public void addToDatabase(Student student) {
        this.studentRepository.saveAndFlush(student);

    }

    public Student getStudentByName(String name) {
        return studentRepository.findByName(name);
    }
}
@Service
public class DepartmentService {
    private final DepartmentRepository departmentRepository;

    @Autowired
    public DepartmentService(DepartmentRepository departmentRepository) {
        this.departmentRepository = departmentRepository;

    }

    public void addToDataBase(List<Department> department) {
        this.departmentRepository.save(department);
        department.forEach(this.departmentRepository::saveAndFlush);
    }
    public Department getDepartmentByName(String name){
        return  this.departmentRepository.findByName(name);
    }
}

我的主要方法

@Component
public class Terminal implements CommandLineRunner {
    private final StudentService studentService;
    private final DepartmentService departmentService;

    @Autowired
    public Terminal(StudentService studentService, DepartmentService departmentService) {
        this.studentService = studentService;
        this.departmentService = departmentService;
    }

        @Override
        public void run(String... strings) throws Exception {
            Department department = new Department("dep1");
            Department department1 = new Department("dep2");
            Department department2 = new Department("dep3");
            Department department3 = new Department("dep4");
            List<Department> departments = new ArrayList<>(Arrays.asList(department, department1, department2, department3));
            this.departmentService.addToDataBase(departments);
    //
            Student student = new Student("pesho", department);
            Student student11 = new Student("gosho", department1);
            this.studentService.addToDatabase(student11);
            this.studentService.addToDatabase(student);
            student = new Student("sasho", department2);
            this.studentService.addToDatabase(student);
    //        System.out.println(this.studentService.getStudentByName("gosho").getDepartment1());
    //        System.out.println("CHECKING ONE TO ONE BIDIRECTIONAL: " + this.departmentService.getDepartmentByName("dep1").getStudent());

        }
    }

因此,当我尝试在学生表中添加学生时,会出现错误 错误是休闲

Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: app.models.Department

【问题讨论】:

  • 谁是关系的所有者?
  • 从学生到系的外键。有什么地方我可以用 JPA 学习 spring boot 数据
  • 你能在部门顶部添加@JoinColumn吗?

标签: java spring hibernate spring-data-jpa


【解决方案1】:

您在 Student 类中为 Department 添加了 cascade= CascadeType.ALL 并将部门分开保存。 this.departmentService.addToDataBase(departments);

修复:不要调用

departmentService.addToDataBase(departments);

或从学生中删除 CascadeType.ALL

【讨论】:

  • 为什么每个人在他们的例子中都添加了级联类型,而当我添加它时它会破坏一切?
  • 它并没有打破所有,你添加级联类型并尝试管理部门。你混合了两个概念。如果你只添加级联,它应该可以工作。如果您只与学生分开的部门工作,它也应该有效(在保存学生期间不要尝试保存部门)
  • 您有没有同时具有级联类型和托管级联实体的示例?
  • sbjavateam 我先添加部门然后添加学生?在我的主要方法中
  • 例如,您在主班学生中有java2s.com/Tutorial/Java/0355__JPA/OneToManyBidirectional.htm 已保存 - em.persist(student);但没有相同的部门,没有 em.persist(dept)。部门是作为学生的一部分创建的
【解决方案2】:

嗯,我不能完全理解你的问题,但这是我想补充的。添加操作的级联未实现或不完整。希望对您有所帮助。

【讨论】:

  • 我可以邀请在stackoverflow中聊天一秒钟,向我解释级联类型吗
  • Well Cascade 类型注解用于关系上,以自动对其子关系实体执行插入删除等操作。
猜你喜欢
  • 2021-09-15
  • 2021-07-14
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 2018-05-07
  • 1970-01-01
  • 2017-05-09
  • 2021-11-15
相关资源
最近更新 更多