This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
//First Unit of work EntityManager em1 = emf.createEntityManager(); EntityTransaction tx1 = em1.getTransaction(); tx1.begin(); ... ScheduledCourse scheduled_course = new ScheduledCourse(...); ... em1.persist(scheduled_course); ... tx1.commit(); em1.close(); emf.close();
Up to this point everything is fine. In my second unit of work I try to remove the instance of that object from the database:
EntityManagerFactory emf2 = Persistence.createEntityManagerFactory("scholastic"); EntityManager em2 = emf2.createEntityManager(); EntityTransaction tx2 = em2.getTransaction(); tx2.begin(); ... //Retrieve the entity instance from the dbase and make it persistent again ScheduledCourse sc = em2.find(ScheduledCourse.class, new Long(1)); ... em2.remove(sc); ... tx2.commit(); em2.close(); emf2.close();
When the delete is attempted, I get this error message:
[java] Could not synchronize database state with session [java] org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
Could someone please explain what I may have done wrong? Why isn't the deletion occurring properly?
I believe I found the answer. It has to do with the fact that the ScheduledCourse object has a collection of enrolled students. Therefore, hibernate tried, not only to perform a delete on the ScheduledCourse instance within the database but also on those instances in the ENROLL table of the database containing references to the students enrolled in the scheduled course.
Which brings me to me next series of questions which I have in a new post here
Perhaps you can help set me straight on my understanding of how persistence works.
Alan [ February 10, 2007: Message edited by: Alan Shiers ]