6 Replies Latest reply: Jan 7, 2011 8:33 AM by cdelahun RSS

    more on JPa delete

    Hjava
      have some time today, so try JPA anagin I get delete by use NativeQuery
      but it suppose have two other ways
      option one
      I hard code it and I get
      1)Query query=em.createQuery(" delete FROM student p WHERE p.userId =1570");
      I get Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails
      the student is a parent table , in normal sql I can't delete from the children table , but if I delete from the parent table all relate records in child just go , why I can't use this ideal in JPa??
      option two
      public class StudentFormService {
      protected EntityManager em;

      public StudentFormService(EntityManager em) {
      this.em = em;
      }
      public void removeStudent(int id) {
      Student emp = findStudent(id);
      if (emp != null) {
      em.remove(emp);
      }
      }

      public Student findStudent(int userId) {
      return em.find(student.class, userId);
      }
      then in my servlet How should I call the remove in servlet
      I have
      Student SD =studentDBO.findStudent(deleteId);
      em.getTransaction().begin();
      SD=studentDBO.removeStudent(deleteId);*/
      not ideal how to continue
        • 1. Re: more on JPa delete
          612864
          HJava,
          You don't mention your object model detals (? is a @OneToMany from Student, Student is a @ManyToOne from ? - owner if bidirectional) Try modifying your defaults for the following
          1) cascade=CascadeType.[] on the relationship mapping - try using CascadeType.MERGE (or at least everything except REMOVE)
          2) orphanRemoval=false on the relationship mapping (new to JPA 2.0)
          3) @PrivateOwned should not be on the relationship if you have it.

          thank you
          /michael
          http://www.eclipselink.org
          • 2. Re: more on JPa delete
            Hjava
            Thank you for the reply ,
            my Student and DocStatus is one to one  relationship , In Db if I delete the roe in student then all his relate children get delete , but when I switch it to jpa I get error ?
            it looks like tables relation in jpa is different with in sql !!

            in my student I already have
            @OneToOne(cascade = CascadeType.ALL, mappedBy = "Student")
            private DocStatus docStatus;
            in my DocStatus
            @OneToOne(optional = false)
            private Student student;
            I use top link

            Edited by: Hjava on Jan 3, 2011 6:53 PM
            • 3. Re: more on JPa delete
              cdelahun
              Hello,

              Sounds like you have a ON DELETE CASCADE option on when you are using SQL to delete a Student as I do not understand why a native SQL delete would delete rows in other tables otherwise.
              As for the JPQL delete query, the JPA specification states that for bulk deletes: "A delete operation only applies to entities of the specified class and its subclasses. It does not cascade to related entities."
              So you will need to manually perform a delete query on the DocStatus entities, or use object level deletes to remove Student Entities for the cascade to be applied.

              EclipseLink does have support for database level ON DELETE CASCADE via the EclipseLink @CascadeOnDelete annotation mentioned by James here http://old.nabble.com/CASCADE-DELETE-at-DB-side-td30425056.html
              and described here:
              http://wiki.eclipse.org/EclipseLink/DesignDocs/324341


              Best Regards,
              Chris
              • 4. Re: more on JPa delete
                cdelahun
                Your first post mentions trying to remove Student entities. Try using

                Student SD =studentDBO.findStudent(deleteId);
                em.getTransaction().begin();
                em.remove(SD);
                em.getTransaction().commit();

                Since you have the cascade option set on Student to docStatus, it should also remove the referenced docStatus.

                Best Regards,
                Chris
                • 5. Re: more on JPa delete
                  Hjava
                  Thank you for the reply,
                  just have little time come back here ! I get the *.NullPointerException*
                  Student SD=studentDBO.findStudent(deleteId);
                  I checked
                  String temp=Click.substring(2,position); //ok
                  int deleteId = Integer.parseInt(temp);//ok
                  // In Student class I have
                  public class StudentFormService {
                  private EntityManager em;

                  public StudentFormService(EntityManagerFactory emf) throws Exception {
                  em =emf.createEntityManager();
                  }
                  public void removeStudent(int id) {
                  Student emp = findStudent(id);
                  if (emp != null) {
                  em.remove(emp);
                  }
                  }

                  public Student findStudent(int userId) {
                  return em.find(Student.class, userId);
                  }


                  What I miss??
                  by the way the cascade between my table is work .
                  Thank You!

                  The .NullPointerException Point to*
                  Student SD=studentDBO.findStudent(deleteId);

                  Edited by: Hjava on Jan 10, 2011 5:42 PM

                  Edited by: Hjava on Jan 10, 2011 5:43 PM
                  • 6. Re: more on JPa delete
                    cdelahun
                    Sorry, can't be much help with a NullPointerException without information on where it is coming from or what is null.
                    The exception itself should have the stack and the line number that is throwing the exception which you will need to use to determine what is null. Check that you have correctly instantiated the object and are not catching/ignoring exceptions that might be thrown preventing it from being instantiated.

                    Regards,
                    Chris