eclipselink - Why is JPA Optimistic Locking incrementing my entity version (@Version) twice during one transaction? -


i developing java ee application using glassfish 4.1, eclipselink , derby , noticed today when create new person entity version of newly created entity 2 instead of 1. following person entity.

@entity public class person {      @id     private long id;     @version     private integer version;      private string name;      @onetoone     private portrait portrait;      @onetomany(mappedby = "person")     private list<portrait> portraits;      ...  } 

the following portrait entity.

@entity public class portrait {      @id     private long id;     @version     private integer version;      private string name;      @manytoone     private person person;      ...  } 

the following part of stateless ejb createperson method results in newly created person entity version of 2.

person person = new person(); person.setname("fred smith");  em.persist(person);  portrait portrait = new portrait(); portrait.setname("fred smith portrait"); portrait.setperson(person);  portrait = portraitmanager.createportrait(portrait);  //portraitmanager stateless ejb.  person.setportrait(portrait);    // commenting out line results in newly created person entity version of 1. person.getportraits().add(portrait); 

if comment out person.setportrait(portrait) line above person entity created version of 1.

i read in other posts jpa increment version of entity if field changes or owned relationship changes. after method returns createportrait method, changing owned relationship still in same transaction don't see why jpa needs increment version.

why jpa increment version on person entity twice during same transaction?

as requested in comments, turned on eclipselink logging , observed following during normal flow:

  1. application starts createperson method.
  2. eclipselink inserts row person table.
  3. application starts createportrait method.
  4. eclipselink inserts row portrait table.
  5. application completes createportrait method.
  6. application associates newly created portrait person.
  7. eclipselink updates row in person table setting version 2 , portrait_id newly created portrait.
  8. application completes createperson method.

if comment out "person.setportrait(portrait);" command, log shows me following:

  1. application starts createperson method.
  2. eclipselink inserts row person table.
  3. application starts createportrait method.
  4. eclipselink inserts row portrait table.
  5. application completes createportrait method.
  6. application completes createperson method.

to more specific question, why eclipselink update version in step 7 of normal flow? eclipselink created row version 1 in step 2 , there no way transaction updated row because row exists in transaction until commits.

i suspect answer way eclipselink choose work under scenario.


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -