How to call EntityListener on embeddable

I have an object with EmebeddedId . Listener entity (evens off whitepace Strings boot) to essentially run as expected, the same listener on id Embeddable not start at all.

Am I doing it wrong? How can this be fixed?

Entity:

 @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; ... 

Built-in:

 @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; ... 

LISTENER:

 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()); } } } ... 
+4
source share
2 answers

I think this is clearly ignored, because this is not the standard place that JPA 2.0 expects. According to the final specification of JPA 2.0, an entity listener can be an entity, an associated superclass, or a listener associated in one of them (see Section 3.5 of the specification):

The method can be assigned as a lifecycle callback method to receive notification about the life cycle of an Event object. The lifecycle callback method can be defined in an entity class mapped to a superclass or object, a listener class associated with an entity or mapped superclass

+3
source

I adapted the EntityListener to recursively observe fields that are annotated using Embeddable . Now, if the object uses a listener, all embedded classes are also processed:

 public class TrimListener { @PostLoad public void trimAfterLoad(final Object entity) throws IllegalAccessException { final Set<Trimmable> trimProps = getTrimProperties(entity); for (final Trimmable trimmable : trimProps) { final String propertyValue = (String) trimmable.field.get(trimmable.target); if (propertyValue != null) { trimmable.field.set(trimmable.target, propertyValue.trim()); } } } private Set<Trimmable> getTrimProperties(final Object entity) throws IllegalAccessException { final Class<?> entityClass = entity.getClass(); final Set<Trimmable> propertiesToTrim = new HashSet<Trimmable>(); for (final Field field : entityClass.getDeclaredFields()) { if (field.getType().equals(String.class) && (field.getAnnotation(Trim.class) != null)) { field.setAccessible(true); propertiesToTrim.add(new Trimmable(entity, field)); // if the entity contains embeddables, propagate the trimming } else if (field.getType().getAnnotation(Embeddable.class) != null) { field.setAccessible(true); propertiesToTrim.addAll(getTrimProperties(field.get(entity))); } } return propertiesToTrim; } private class Trimmable { final Object target; final Field field; public Trimmable(final Object target, final Field field) { this.target = target; this.field = field; } } } 
+2
source

Source: https://habr.com/ru/post/1446221/


All Articles