Jim,

I tried your approach (I think) but got the following exception:

*<openjpa-2.0.1-r422266:989424 nonfatal user error>
> org.apache.openjpa.persistence.InvalidStateException: Encountered unmanaged
> object in persistent field
> "se.digia.maia.common.persistence.EntityBase.auditLog<element:class
> se.digia.maia.common.auditlog.domain.AuditLog>" during flush.  However, this
> field does not allow cascade persist. Set the cascade attribute for this
> field to CascadeType.PERSIST or CascadeType.ALL (JPA annotations) or
> "persist" or "all" (JPA orm.xml), or enable cascade-persist globally, or
> manually persist the related field value prior to flushing. You cannot flush
> unmanaged objects or graphs that have persistent associations to unmanaged
> objects.
> **FailedObject: se.digia.maia.common.auditlog.domain.AuditLog@39bc82
> ** at
> org.apache.openjpa.kernel.SingleFieldManager.preFlushPC(SingleFieldManager.java:767)
> ** at
> org.apache.openjpa.kernel.SingleFieldManager.preFlushPCs(SingleFieldManager.java:748)
> ** at
> org.apache.openjpa.kernel.SingleFieldManager.preFlush(SingleFieldManager.java:642)
> ** at
> org.apache.openjpa.kernel.SingleFieldManager.preFlush(SingleFieldManager.java:575)
> ** at
> org.apache.openjpa.kernel.SingleFieldManager.preFlush(SingleFieldManager.java:491)
> ** at
> org.apache.openjpa.kernel.StateManagerImpl.preFlush(StateManagerImpl.java:2957)
> ** at
> org.apache.openjpa.kernel.PDirtyState.beforeFlush(PDirtyState.java:38)
> ** at
> org.apache.openjpa.kernel.StateManagerImpl.beforeFlush(StateManagerImpl.java:1047)
> ** at org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:2077)
> ** at org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:2037)
> ** at
> org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(BrokerImpl.java:1955)
> ** at
> org.apache.openjpa.kernel.LocalManagedRuntime.commit(LocalManagedRuntime.java:81)
> ** at org.apache.openjpa.kernel.BrokerImpl.commit(BrokerImpl.java:1479)
> ** at
> org.apache.openjpa.kernel.DelegatingBroker.commit(DelegatingBroker.java:925)
> ** at
> org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:560)
> ** at
> se.digia.maia.core.bu.test.CRUDTest.updateBusinessUnitTest(CRUDTest.java:141)
> *


I use OpenJPA 2.0.1. This is my relationship to the audit log table:

  @OneToMany
  @JoinTable(
    name = "ENTITY_ALOG",
    joinColumns = @JoinColumn(name = "ENTITY_ID"),
    inverseJoinColumns = @JoinColumn(name = "ALOG_ID"))
  private Collection<AuditLog> auditLog;

This is my @PreUpdate method:

  @PreUpdate
  public void beforeUpdate() {
    setUpdatedWhen(Calendar.getInstance());
    setUpdatedBy(Session.getCurrentUser());
    if (this.getClass().getAnnotation(Audit.class) != null) {
      AuditLog al = new AuditLog();
      al.setLogEntry("serialization of object");
      auditLog.add(al);
    }
  }

OpenJPA doesn't seem to like this. What did you do differently?

/Bengt

2011/7/12 Bengt Rodehav <be...@rodehav.com>

> OK - I didn't know that.
>
> I agree that it would be nice if the OpenJPA guys would support this
> behaviour. My experience of standards (like JPA) is that the standard itself
> is rarely enough for production purposes. Things outside of the standard
> need to be in place as well. Then, after a while, the standard will
> sometimes adapt to reality and incorporate what has become the de facto
> standard.
>
> In this case, it is clear that the behaviour of the life-cycle callback
> methods is not defined enough in the standard. The implementations should
> therefore create a de facto standard. Let's hope they do...
>
> /Bengt
>
> 2011/7/12 Jim Talbut <jtal...@spudsoft.co.uk>
>
>> On 12/07/2011 09:33, Bengt Rodehav wrote:
>>
>>> Why do you think that your approach is not supported by JPA? From other
>>> posts on this list I've seen that you are allowed to add/enrich your
>>> entity
>>> in the PreUpdate/PreCreate callbacks. That's all you are doing right.
>>> Then
>>> JPA persists your enriched entities.
>>>
>>
>> I asked on this list a few months ago:
>> http://web.archiveorange.com/**archive/v/tXrrxw9bXkttUykS7B6K<http://web.archiveorange.com/archive/v/tXrrxw9bXkttUykS7B6K>
>>
>> You can change the state of entities, but creating new entities is not
>> permitted by the JPA spec.
>> At the moment it does work, and I now what I'm going to do if it stops
>> working, so I'm happy.
>> I'd be happier if OpenJPA explicitly supported it, but it doesn't keep me
>> awake at night.
>>
>> Jim
>>
>
>

Reply via email to