I have several Entity classes. Now I want to use @PostLoad in every entity. The function code is for every entity the same. So I made a super class (public abstract class AbstractModel {..}) which contain the @PostLoad method. Now is it possible to do this? In a quick test the method is not executed but the method is executed placed in the entity… Maybe someone has a workaround for this?
@MappedSuperclass
public class Parent implements Serializable
{
@PostLoad
public void postLoad()
{
System.out.println("postLoad called!");
}
}
My entity class who extends from Parent:
@Entity
@Table(name = "albums") //de naam van de tabel wordt 'albums'
public class Album extends Parent implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id") //de naam van de kolom wordt 'id'
private Long id;
...
}
And the error when I’m running it:
Exception [EclipseLink-30005] (Eclipse
Persistence Services –
2.0.2.v20100323-r6872): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception
was thrown while searching for
persistence archives with ClassLoader:
sun.misc.Launcher$AppClassLoader@11b86e7Internal Exception:
javax.persistence.PersistenceException:
Exception [EclipseLink-28018] (Eclipse
Persistence Services –
2.0.2.v20100323-r6872): org.eclipse.persistence.exceptions.EntityManagerSetupExceptionException Description: Predeployment
of PersistenceUnit [JPA_TestPU]
failed.Internal Exception: Exception
[EclipseLink-7161] (Eclipse
Persistence Services –
2.0.2.v20100323-r6872): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Entity class
[class jpatest.Album] has no primary
key specified. It should define either
an @Id, @EmbeddedId or an @IdClass. If
you have defined PK using any of these
annotations then make sure that you do
not have mixed access-type (both
fields and properties annotated) in
your entity class hierarchy.
At least “Java Persistence with JPA” says that this should work when applied to a mapped super class (mark AbstractModel with @MappedSuperClass).