gpt4 book ai didi

java - 如何在可嵌入对象上触发 EntityListener

转载 作者:搜寻专家 更新时间:2023-11-01 02:29:17 26 4
gpt4 key购买 nike

我有一个带有 EmebeddedId 的实体。实体上的实体监听器(加载时去除 Strings 上的空白)按预期被触发,id Embeddable 上的同一监听器根本没有被触发。

我做错了吗?如何修复?

实体:

@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 最终规范,实体监听器可以是实体、映射的父类(super class)或与其中之一关联的监听器(请参阅规范的第 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

关于java - 如何在可嵌入对象上触发 EntityListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13397763/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com