Spring Data JPA ManyToOne双向

时间:2017-07-06 17:17:59

标签: java spring hibernate spring-data-jpa

问题:它一直有效,直到我尝试将Student obejcts添加到Database,但正在正确创建表。 我无法进一步简化帖子。但它主要是代码不需要大量阅读,它是一个简单的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);
}

SERVICES

@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

2 个答案:

答案 0 :(得分:1)

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

修复:不要打电话

departmentService.addToDataBase(departments);

或从学生

中删除CascadeType.ALL

答案 1 :(得分:0)

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