title: Spring Data JPA ManyToOne Bidirectional
body: <p>Problem: it works till i try to add Student obejcts to Database, but the tables are being created correctly .
  I can't simplify the post any further. But it's mainly code that doesn't require a lot of reading, it's a simple spring data repository service model. I posted it all due to the fact idk what am i doing wrong. Problem is in the JPA mapping.
  I got the example from over here <a href="http://www.java2s.com/Tutorial/Java/0355__JPA/OneToManyBidirectional.htm" rel="nofollow noreferrer">http://www.java2s.com/Tutorial/Java/0355__JPA/OneToManyBidirectional.htm</a></p>
  
  <p>MDOELS</p>
  
  <pre><code>@Entity
  public class Department {
      @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
      private int id;
      private String name;
  
      @OneToMany(mappedBy="department")
      private Collection&lt;Student&gt; 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&lt;Student&gt; getStudents() {
          return students;
      }
  
      public void setStudent(Collection&lt;Student&gt; 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;
      }
  }
  </code></pre>
  
  <p>REPOSITORIES</p>
  
  <pre><code>@Repository
  public interface DepartmentRepository extends JpaRepository&lt;Department, Integer&gt; {
      Department findByName(String name);
  
  }
  
  @Repository
  public interface StudentRepository extends JpaRepository&lt;Student, Integer&gt; {
      Student findByName(String name);
  }
  </code></pre>
  
  <p>SERVICES</p>
  
  <pre><code>@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&lt;Department&gt; department) {
          this.departmentRepository.save(department);
          department.forEach(this.departmentRepository::saveAndFlush);
      }
      public Department getDepartmentByName(String name){
          return  this.departmentRepository.findByName(name);
      }
  }
  </code></pre>
  
  <p>My main method</p>
  
  <pre><code>@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&lt;Department&gt; departments = new ArrayList&lt;&gt;(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());
  
          }
      }
  </code></pre>
  
  <p>So here when i try to add students in the students table it gives an error 
  The error is the fallowing</p>
  
  <pre><code>Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: app.models.Department
  </code></pre>