Please help me explain the following (for me) very strange JPA behavior. I intentionally change the primary key of an object that is prohibited in JPA.
So, first, commit correctly throws "Exception Description: the [date] attribute of the [some.package.Holiday] class is mapped to the primary key column in the database. Updates are not allowed.".
But the second (third, fourth, ...) success ...! How is this possible ?!
Holiday h1 = EM.find(Holiday.class, new GregorianCalendar(2011, 0, 3).getTime()); try { EM.getTransaction().begin(); h1.setDate(new GregorianCalendar(2011, 0, 4).getTime()); EM.getTransaction().commit(); System.out.println("First commit succeed"); } catch (Exception e) { System.out.println("First commit failed"); } try { EM.getTransaction().begin(); EM.getTransaction().commit(); System.out.println("Second commit succeed"); } catch (Exception e) { System.out.println("Second commit failed"); }
Will be printed:
First commit failed Second commit succeed
OMG, how is that possible ?!
(Using EclipseLink 2.2.0.v20110202-r8913 with MySQL.)
source share