超类中的@PostLoad [英] @PostLoad in super class
问题描述
我有几个Entity类.现在,我想在每个实体中使用@PostLoad.每个实体的功能代码都相同.因此,我制作了一个包含@PostLoad方法的超类(公共抽象类AbstractModel {..}).现在可以这样做吗?在快速测试中,该方法未执行,但是该方法被放置在实体中...也许有人对此有解决方法?
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;
...
}
运行时出现错误:
异常[EclipseLink-30005](Eclipse 持续服务- 2.0.2.v20100323-r6872):org.eclipse.persistence.exceptions.PersistenceUnitLoadingException 异常描述:异常 在搜寻时被抛出 使用ClassLoader的持久性存档: sun.misc.Launcher$AppClassLoader@11b86e7
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@11b86e7
内部异常: javax.persistence.PersistenceException: 异常[EclipseLink-28018](Eclipse 持续服务- 2.0.2.v20100323-r6872):org.eclipse.persistence.exceptions.EntityManagerSetupException
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.0.2.v20100323-r6872): org.eclipse.persistence.exceptions.EntityManagerSetupException
异常说明:预部署 PersistenceUnit [JPA_TestPU] 失败的.
Exception Description: Predeployment of PersistenceUnit [JPA_TestPU] failed.
内部异常:异常 [EclipseLink-7161](Eclipse 持续服务- 2.0.2.v20100323-r6872):org.eclipse.persistence.exceptions.ValidationException 异常描述:实体类 [class jpatest.Album]没有主要语言 指定的密钥.它应该定义 @ Id,@ EmbeddedId或@IdClass.如果 您已经使用以下任何一种定义了PK 注释,然后确保您执行 没有混合访问类型(两者 带有注释的字段和属性) 您的实体类层次结构.
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.
推荐答案
至少使用JPA的Java持久性"说这适用于映射的超类(带有@MappedSuperClass的AbstractModel).
At least "Java Persistence with JPA" says that this should work when applied to a mapped super class (mark AbstractModel with @MappedSuperClass).
这篇关于超类中的@PostLoad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!