Hi,
I just added a test case that demonstrates that this works correctly
--- see https://svn.apache.org/viewvc?view=rev&revision=559515
What version of OpenJPA are you using? Until about a month ago, I
think that the beforePersist() callback wasn't being invoked at all,
but that would not result in the symptoms that you're describing.
-Patrick
On 7/25/07, Prashant Bhat <[EMAIL PROTECTED]> wrote:
Just to verify this, I added following two properties to
org.apache.openjpa.persistence.fields.EnumFieldType
private Date createdOn;
private boolean active = true;
And modified TestEnumQueryConstructor :
setup() {
if (emf instanceof OpenJPAEntityManagerFactory) {
((OpenJPAEntityManagerFactory) emf).addLifecycleListener(new
EntityLifecycleHandler(), (Class[]) null);
}
....
}
public void testEnumQueryConstructor() {
..
assertEquals(SampleEnum.BAR, e.getEnumField());
assertNotNull(e.getCreatedOn());
assertFalse(e.isActive());
}
public class EntityLifecycleHandler extends AbstractLifecycleListener {
@Override
protected void eventOccurred(LifecycleEvent event) {
switch (event.getType()) {
case LifecycleEvent.BEFORE_PERSIST:
if(event.getSource() instanceof EnumFieldType) {
EnumFieldType e = (EnumFieldType)event.getSource();
e.setCreatedOn(new Date());
e.setActive(false);
e.setEnumField(SampleEnum.BAR);
}
break;
default:
break;
}
}
}
Both of these fail:
assertNotNull(e.getCreatedOn());
assertFalse(e.isActive());
But assertEquals(SampleEnum.BAR, e.getEnumField()); passes the test!
Sorry for not giving a full test case as I'm not very familiar with it! Hope
this helps.
Thanks and Regards,
Prashant
On 7/25/07, Prashant Bhat <[EMAIL PROTECTED]> wrote:
>
> Hi,
>
> I've a mapped super class 'BaseEntity' that has common properties like
> Date createdOn; Date updatedOn; boolean active; Company company; User
> createdBy; etc.
>
> Initially I used @EntityListeners defined at the BaseEntity and in
> LifeCycleEvent handler on PrePersist(), I update these above common
> properties.
> I wanted my own instance of the listener(or to get hold of it), as I've to
> update these from the UI module. But I couldn't find a way to do this in JPA
> as it accepts only classes. Thanks to OpenJPA, I could achieve this easily
> like this:
>
> if (entityManagerFactory instanceof OpenJPAEntityManagerFactory) {
> ((OpenJPAEntityManagerFactory)
> entityManagerFactory).addLifecycleListener(entityLifecycleHandler, (Class[])
> null);
> }
>
> But in both the above cases, inside prePersist method, If I set the
> properties of type Date or Boolean, they do not get saved.
> Other properties like Company, User, etc. are saved properly.
>
> Are the entities passed can not be modified in the listener? Or Is there
> any other approach that could achieve this?
>
> Thanks and Regards,
> Prashant
>
>
--
Patrick Linskey
202 669 5907