Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 311395 - Multiple lifecycle callback methods for the same lifecycle event
Summary: Multiple lifecycle callback methods for the same lifecycle event
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows XP
: P2 normal (vote)
Target Milestone: ---   Edit
Assignee: Guy Pelletier CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-05-03 13:10 EDT by Tommy CLA
Modified: 2022-06-09 10:08 EDT (History)
3 users (show)

See Also:


Attachments
Proposed changes (72.02 KB, patch)
2010-07-15 11:51 EDT, Guy Pelletier CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tommy CLA 2010-05-03 13:10:15 EDT
If I create an @Entity inherited from a @MappedSuperclass and both of them have  independent (no method overriding) but the same lifecycle callback method, for example @PrePersist, the following exception occur.

Internal Exception: Exception [EclipseLink-7227] (Eclipse Persistence Services - 2.0.2.v20100323-r6872): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The listener class [class my.MyEntity] has multiple lifecycle callback methods for the same lifecycle event ([private void my.MyMappedSuperclass.myMappedSuperclassPrePersist()] and [private void my.MyEntity.myEntityPrePersist()]).
        at org.eclipse.persistence.exceptions.EntityManagerSetupException.deployFailed(EntityManagerSetupException.java:218)
        ... 61 more
Caused by: Exception [EclipseLink-7227] (Eclipse Persistence Services - 2.0.2.v20100323-r6872): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The listener class [class my.MyEntity] has multiple lifecycle callback methods for the same lifecycle event ([private void my.MyMappedSuperclass.myMappedSuperclassPrePersist()] and [private void my.MyEntity.myEntityPrePersist()]).
        at org.eclipse.persistence.exceptions.ValidationException.multipleLifecycleCallbackMethodsForSameLifecycleEvent(ValidationException.java:2246)
        at org.eclipse.persistence.internal.jpa.metadata.listeners.EntityListener.addEventMethod(EntityListener.java:120)
        at org.eclipse.persistence.internal.jpa.metadata.listeners.EntityClassListener.addEventMethod(EntityClassListener.java:46)
        at org.eclipse.persistence.internal.jpa.metadata.listeners.EntityListener.setPrePersistMethod(EntityListener.java:462)
        at org.eclipse.persistence.internal.jpa.metadata.listeners.EntityListenerMetadata.setPrePersist(EntityListenerMetadata.java:475)
        at org.eclipse.persistence.internal.jpa.metadata.listeners.EntityListenerMetadata.processCallbackMethods(EntityListenerMetadata.java:383)
        at org.eclipse.persistence.internal.jpa.metadata.listeners.EntityClassListenerMetadata.process(EntityClassListenerMetadata.java:74)
        at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.processListeners(EntityAccessor.java:1068)
        at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.addEntityListeners(MetadataProcessor.java:104)
        at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:327)


According to the JPA specifications 1.0 and 2.0, this is a legitimate use case (2.11.2 Mapped Superclasses) and the behavior of multiple lifecycle callback methods is also specified. (3.5.4 Multiple Lifecycle Callback Methods for an Entity Lifecycle Event)


My test classes to reproduce this bug.

=== CUT ===
package my;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;

@MappedSuperclass
public abstract class MyMappedSuperclass {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private long id;
  
  private String createdBy;
  
  @PrePersist
  private void myMappedSuperclassPrePersist() {
    if (createdBy == null) {
      createdBy = "root";
    }
  }
}

=== CUT ===
package my;

import javax.persistence.Entity;
import javax.persistence.PrePersist;

@Entity
public class MyEntity extends MyMappedSuperclass {

  private String data;
  
  @PrePersist
  private void myEntityPrePersist() {
    if (data == null) {
      data = "NO DATA";
    }
  }

}

=== CUT ===
Comment 1 Tom Ware CLA 2010-05-13 09:53:00 EDT
Setting target and priority.  See the following page for details of what these values mean.

http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines
Comment 2 Guy Pelletier CLA 2010-07-15 11:51:01 EDT
Created attachment 174415 [details]
Proposed changes

Change includes some clean up to the jpa.xml.merge.advanced test model.
Comment 3 Guy Pelletier CLA 2010-07-16 08:25:51 EDT
Changes have been submitted.

Reviewed by: Michael O'Brien

Tests: New test (testPreAndPostPersistExpertBeerConsumer) was added to the InheritedCallbacksJunitTest includes model changes to the inherited model.

Patch also includes some clean up to the xml.merge.advanced model.
Comment 4 Eclipse Webmaster CLA 2022-06-09 10:08:48 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink