如何在Embeddable上触发EntityListener

我有一个带有EmebeddedId的实体.正在按预期触发实体的实体监听器(在加载时修剪字符串上的空格),ID ased上的相同监听器根本不被触发.

我做错了吗?怎么修好?

实体:

@Entity
@Table(name = "SUBMITTER_VIEW")
@EntityListeners(TrimListener.class)
public class Submitter implements Serializable {

    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private SubmitterPK id;

    @Trim
    @Column(name = "DOC_NAME")
    private String name;
...

嵌入式:

@Embeddable
@EntityListeners(TrimListener.class)
public class SubmitterPK implements Serializable {

    private static final long serialVersionUID = 1L;

    @Column(name = "LSTORT")
    private String bsnr;

    @Trim
    @Column(name = "LOGIN")
    private String login;
...

监听器:

public class TrimListener {

    Logger log = LoggerFactory.getLogger("TrimListener");

    @PostLoad
    public void repairAfterLoad(final Object entity) throws IllegalAccessException {

        log.debug("trimlistener active");

        final Set<Field> trimProps = getTrimProperties(entity.getClass());

        for (final Field fieldToTrim : trimProps) {
            final String propertyValue = (String) fieldToTrim.get(entity);
            if (propertyValue != null) {
                fieldToTrim.set(entity, propertyValue.trim());
            }
        }
    }
...
最佳答案
我认为它显然被忽略了,因为它不是JPA 2.0所期望的标准位置.根据JPA 2.0最终规范,实体监听器可以是实体,映射的超类或与其中一个相关联的监听器(参见规范的第3.5节):

A method may be designated as a lifecycle callback method to receive notification of entity lifecycle
events. A lifecycle callback method can be defined on an entity class, a mapped superclass, or an entity
listener class associated with an entity or mapped superclass

转载注明原文:如何在Embeddable上触发EntityListener - 代码日志