openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Prashant Bhat" <prashant...@gmail.com>
Subject Re: Modifications to Properties of Basic types inside LifecycleListener do not get saved!
Date Wed, 25 Jul 2007 11:05:33 GMT
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 <prashant.mr@gmail.com> 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
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message