File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Could not synchronize Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Could not synchronize " Watch "Could not synchronize " New topic

Could not synchronize

Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
Hi there,

I'm testing a small app while trying to learn Java Persistence . Just a newbie at this...

In my first unit of work I create and persist a ScheduledCourse object like so:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("scholastic");

//First Unit of work
EntityManager em1 = emf.createEntityManager();
EntityTransaction tx1 = em1.getTransaction();
ScheduledCourse scheduled_course = new ScheduledCourse(...);

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();
//Retrieve the entity instance from the dbase and make it persistent again
ScheduledCourse sc = em2.find(ScheduledCourse.class, new Long(1));

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?

Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Not sure why it isn't working for you.

But I do want to point out that EntityManagerFactory classes are heavy weight and should only be created once, and re-used to get EntityManagers when needed.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
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.

[ February 10, 2007: Message edited by: Alan Shiers ]
I agree. Here's the link:
subject: Could not synchronize
It's not a secret anymore!