• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Could not synchronize

 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Grow a forest with seedballs and this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic