How to implement preUpdate from DescriptorEventAdapter? [message #503123] |
Thu, 10 December 2009 18:35  |
Eclipse User |
|
|
|
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 #503775 is a reply to message #503123] |
Tue, 15 December 2009 12:18  |
Eclipse User |
|
|
|
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>".
|
|
|
Powered by
FUDForum. Page generated in 0.03072 seconds