openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "seth.jackson" <>
Subject openjpa 1.2.2 - Optimistic Lock Exception
Date Fri, 02 Apr 2010 13:04:12 GMT

I've receiving an Optimistic Lock Exception when performing the following
operations and I'm not quite sure I understand why. Consider the following
(this is just a proof):

public interface Entity implements Serializable{}

@Table(name="ACCOUNT", schema="PROOF")
public class Account implements Entity {
  @Column(name="ACCOUNT_ID", nullable=false)
  private String accountId;

  private String accountDescription;

  @Column(name="VERSION", nullable=false)
  private Long version;

  @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
  @JoinTable(name="ACCOUNT_ACTIVITY", schema="PROOF",
  private Set<Activity> activities = new HashSet<Activity>();

  public Account() {}
  public Account(String accountId, String accountDescription) {
    this.accountId = accountId;
    this.accountDescription = accountDescription;
  /* getters and setters */

@Table(name="ACTIVITY", schema="PROOF")
public class Activity implements Entity {
  @Column(name="ACCOUNT_ID", nullable=false)
  private String accountId;

  @Column(name="SEQ_NUMBER", nullable=false)
  private Long seqNumber;

  private String transactionType;

  private Long amount;

  private Long version;

  public Activity(){}
  public Activity(String accountId, Long seqNumber, String transactionType,
Long amount) {
    this.accountId = accountId;
    this.seqNumber = seqNumber;
    this.transactionType = transactionType;
    this.amount = amount;
  /* getters and setters */

// Entity Manager Handler snippet
public <E extends Entity> void persistEntityList(List<E> entityList) throws
Exception {
  open(); // Opens the entity manager

  for (E ent : entityList) {
    ent = entityManager.merge(ent);

  close(); // Commit transaction and close entity manager

* Test method
public void testTwoTableRelationship throws Exception {
  List<Account> accounts = new ArrayList<Account>();
  accounts.add(new Account("Account1", "Account1 Desc"));
  accounts.add(new Account("Account2", "Account2 Desc"));
  accounts.add(new Account("Account3", "Account3 Desc"));

  List<Activity> activities = new ArrayList<Activity>();
  activities.add(new Activity("Account1", 1L, "Debit", 100L));
  activities.add(new Activity("Account1", 2L, "Credit", 500L));
  activities.add(new Activity("Account2", 1L, "Credit", 300L));
  activities.add(new Activity("Account3", 1L, "Debit", 100L));
  activities.add(new Activity("Account3", 2L, "Debit", 200L));


  accounts = entityManagerHandler.retrieveEntities(Account.class); // this
method builds a Query and retrieves all Accounts from the DB

  for (Account acc : accounts) {
    if (acc.getAccountId().equalsIgnoreCase("account1")) {
      acc.setDescription("NEW ACCOUNT 1 DESCRIPTION");

  entityManagerHandler.persist(accounts); //This throws the exception

So here's the run down:
- This is a detached entity manager; the transaction is opened for operation
then immediately closed (this is why there is a merge() call in the
- The tables are populated with data properly with no problems.
- When persisting the list of Accounts that have an associated Set of
Activity, the exception is thrown.
- I have removed the persist() call in the entityManagerHandler and the
exception still occurs. (This is a separate discussion alone, as I don't
understand why this call is necessary)

Any thoughts on why the exception is being thrown?


Seth Jackson
View this message in context:
Sent from the OpenJPA Users mailing list archive at

View raw message