aspose file tools*
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
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Could not synchronize " Watch "Could not synchronize " New topic
Author

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();
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?

Alan
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

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.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
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.

Alan
[ February 10, 2007: Message edited by: Alan Shiers ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Could not synchronize