openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "seth.jackson" <seth.jackson....@dfas.mil>
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{}


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

  @Column(name="ACCOUNT_DESCRIPTION")
  private String accountDescription;

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

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

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


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

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

  @Column(name="TRANSACTION_TYPE")
  private String transactionType;

  @Column(name="AMOUNT")
  private Long amount;

  @Version
  @Column(name="VERSION")
  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);
    entityManager.persist(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"));

  entityManagerHandler.persistEntityList(accounts);
  
  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));

  entityManagerHandler.persistEntityList(activities);

  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
entityManagerHandler)
- 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?

Thanks,
Seth


-----
Seth Jackson
-- 
View this message in context: http://n2.nabble.com/openjpa-1-2-2-Optimistic-Lock-Exception-tp4842528p4842528.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Mime
View raw message