Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 315154

Summary: Default entity listeners as registered in entity mappings XML file not invoked
Product: z_Archived Reporter: Andy Clan <andy>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: eclipselink.orm-inbox, guy.pelletier, mkienenb, StEwert, tom.ware
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows Vista   
Whiteboard:
Attachments:
Description Flags
Proposed changes none

Description Andy Clan CLA 2010-05-31 21:43:30 EDT
Build Identifier: EclipseLink 2.0.0 Dec 10th 2009

I am writing a simple global debug listener to output CRUD debug events on 4 simple entities. In the entity mappings XML file, I have the following:

    
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
                                     http://java.sun.com/xml/ns/persistence/orm_2_0.xsd" version="2.0">
    <persistence-unit-metadata>
        <persistence-unit-defaults>
            <entity-listeners>
                <entity-listener class="bo.DebugListener">
                    <pre-persist method-name="prePersist"></pre-persist>
                    <post-persist method-name="postPersist"></post-persist>
                    <pre-remove method-name="preRemove"></pre-remove>
                    <post-remove method-name="postRemove"></post-remove>
                    <pre-update method-name="preUpdate"></pre-update>
                    <post-update method-name="postUpdate"></post-update>
                    <post-load method-name="postLoad"></post-load>
                </entity-listener>
            </entity-listeners>
        </persistence-unit-defaults>
    </persistence-unit-metadata>
</entity-mappings>


My DebugListener class just contains simple sys out calls as follows:



public class DebugListener {

    public DebugListener() {
        System.out.println(">>>>>>>>>>>>>>> CONSTRUCTOR");
    }

    public void prePersist(Object o) {
        System.out.println(">>>>>>>>>>>>>>> " + o.getClass() + " PREPERSIST");
    }

    public void postPersist(Object o) {
        System.out.println(">>>>>>>>>>>>>>> " + o.getClass() + " POSTPERSIST");
    }

    public void preUpdate(Object o) {
        System.out.println(">>>>>>>>>>>>>>> " + o.getClass() + " PREUPDATE");
    }

    public void postUpdate(Object o) {
        System.out.println(">>>>>>>>>>>>>>> " + o.getClass() + " POSTUPDATE");
    }

    public void preRemove(Object o) {
        System.out.println(">>>>>>>>>>>>>>> " + o.getClass() + " PREREMOVE");
    }

    public void postRemove(Object o) {
        System.out.println(">>>>>>>>>>>>>>> " + o.getClass() + " POSTREMOVE");
    }

    public void postLoad(Object o) {
        System.out.println(">>>>>>>>>>>>>>> " + o.getClass() + " POSTLOAD");
    }
}


I have 4 entities, several instances of which are inserted into an in-process Derby database (running in a Java SE environment), but the CRUD sys outs never get called. I do get 4 constructor sys outs, however.

Also, if I move the block of <entity-listener> XML into a section for any specific entity (under <entity class="...">), I do see all the CRUD sysouts for that entity. So it seems its a problem with the default listeners. I am thinking that the default entity listeners should be invoked on each and every entity registered into the persistence unit by default with the above XML and listener class.

Ref Community Forums, message #536083

Reproducible: Always

Steps to Reproduce:
1. Write some simple entity classes annotated with @Entity
2. Write a main method to instantiate a couple, and code to use an EntityManager to persist them to a Derby database using the embedded driver and the org.eclipse.persistence.jpa.PersistenceProvider. Declare a default entity listener in the entity-mappings XML file as shown above.
3. Run the Main class. The entities will be peristsed OK, but only the entity listener constructor sys out is called, not any of the pre and post persistence callback methods.
Note: running in a Java SE environment
Comment 1 Tom Ware CLA 2010-06-14 14:28:48 EDT
Setting target and priority.  See the following page for details of what these fields mean:

http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines
Comment 2 Mike Kienenberger CLA 2010-07-22 12:02:27 EDT
From the forums:

=======================
Mon, 31 May 2010 11:55
James
	
There seems to be a bug that if a descriptor does not have any event listeners, then the default event listeners are not triggered.

[...]

A workaround would be to define a SessionCustomizer that iterates over all of the ClassDescriptors and adds a dummy DescriptorEventListener to their eventManagers.
=======================


This code added as a SessionCustomizer worked as a workaround for me:

	public void customize(Session session) throws Exception {
		Map<Class<?>,ClassDescriptor> descriptors = session.getDescriptors();
		Collection<ClassDescriptor> values = descriptors.values();
		for (ClassDescriptor classDescriptor : values) {
			classDescriptor.getEventManager().addListener(new DescriptorEventAdapter());
		}
	}
Comment 3 Guy Pelletier CLA 2010-08-18 11:26:58 EDT
Created attachment 176905 [details]
Proposed changes
Comment 4 Guy Pelletier CLA 2010-08-18 12:57:28 EDT
Changes have been submitted.

Reviewed by: Tom Ware

New test (testDefaultListenerOnMacBook) added to LifecycleCallbackJunitTest.
Comment 5 Eclipse Webmaster CLA 2022-06-09 10:28:29 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink