Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » How to implement preUpdate from DescriptorEventAdapter?
How to implement preUpdate from DescriptorEventAdapter? [message #503123] Thu, 10 December 2009 23:35 Go to next message
Jon.B  is currently offline Jon.B
Messages: 8
Registered: December 2009
Junior Member
While implementing a PreUpdate method to update a timestamp on an entity, I ran into the exact problem described at: http://www.eclipse.org/forums/index.php?t=msg&goto=49975 6&S=3bcc64b0e0822e7707f2ce0b616d92df

I can get PreUpdate to fire, but it fires in the same manner as using the @PreUpdate notation. I need it to fire with the DescriptorEvent call to get at the updateAttributeWithObject method.

I have attempted to implement the method in a listener that extends DescriptorEventAdapter. I can get the code worked out, but how do I register this in the ORM to be called appropriately??

Everything works when I set up the preUpdate method to be compatible with the Entity object but not with the DescriptorEvent. Setting <pre-update> to the preUpdate(DescriptorEvent e) method, fails.

Listener:
public class TimestampListener extends DescriptorEventAdapter {
    //@PreUpdate
    public void setUpdatedOn(Timestamped object) {
        object.setUpdatedOn(new Date());
    }

    @PrePersist
    public void setCreatedOn(Timestamped object) {
        if (object.getCreatedOn() == null) {
            object.setCreatedOn(new Date());
        }
    }

    public void customize(ClassDescriptor descriptor) {
        descriptor.getEventManager().addListener(this);
    }

//THIS IS THE METHOD I WANT TO HIT
    public void preUpdate(DescriptorEvent e) {
        setUpdatedOn((Subject) e.getSource());
        e.updateAttributeWithObject("updated_on", e);
    }
}


ORM XML:
    <entity class="MyEntity">
        <attributes>
            ......
        </attributes>
        <entity-listeners>
            <entity-listener class="com.myproject.listeners.TimestampListener">
                <pre-update method-name="setUpdatedOn"/>
            </entity-listener>
        </entity-listeners>
    </entity>


TIA!


Re: How to implement preUpdate from DescriptorEventAdapter? [message #503293 is a reply to message #503123] Fri, 11 December 2009 17:04 Go to previous messageGo to next message
Jon.B  is currently offline Jon.B
Messages: 8
Registered: December 2009
Junior Member
Solved it.

The Listener class needs to extend DescriptorEventAdapter and implement DescriptorCustomizer. The hook to register that listener with the Entity goes in persistence.xml:
<property name="eclipselink.descriptor.customizer.MYENTITYNAME" value="com.mirth.results.entities.listeners.TimestampListener"/>


Re: How to implement preUpdate from DescriptorEventAdapter? [message #503775 is a reply to message #503123] Tue, 15 December 2009 17:18 Go to previous message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

You need to register the DesriptorEventListener with the descriptor. This can be done using a DescriptorCustomizer. You seem to have the customize() method already, you need to implement DescriptorCustomizer and register the customizer using the @Customizer annotation in your Entity, or the persistence.xml property, "eclipselink.descriptor.customizer.<entity-name>".


James : Wiki : Book : Blog : Twitter
Previous Topic:1.1.3 vs 2.0 equals
Next Topic:JPA 2.0 non-jta-data-source issue
Goto Forum:
  


Current Time: Tue Sep 23 00:31:14 GMT 2014

Powered by FUDForum. Page generated in 0.01849 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software