SDN BeforeSaveEvent <T> captures events before saving entities! = T

Currently I have centralized the creation of Id

each NodeEntity in the application's BeforeSave event. Something like that:

   @Inject
   IdentifierFactory identifierFactory;

   @Bean
   ApplicationListener<BeforeSaveEvent> beforeSaveEventApplicationListener() {
      return new ApplicationListener<BeforeSaveEvent>() {
         @Override
         public void onApplicationEvent(BeforeSaveEvent event) {
            if (event.getEntity() instanceof IdentifiableEntity) {
               IdentifiableEntity entity = (IdentifiableEntity) event.getEntity();
               if (entity.getId() == null) entity.setId(identifierFactory.generateId());
            }
         }
      };

      

This works great.

However, in my chart model, I have identifiable entities (with an id attribute that implements the interface IdentififableEntity

) and other non-identifiable ones. What I am trying to do is capture the event before saving, only if the object is identified, to avoid being instantiated / casting:

if (event.getEntity() instanceof IdentifiableEntity)
IdentifiableEntity entity = (IdentifiableEntity) event.getEntity();

      

I tried the following, but it doesn't work (before saving, the event continues to capture unidentified objects):

   @Bean
   ApplicationListener<BeforeSaveEvent<IdentifiableEntity>> beforeSaveEventApplicationListener() {
      return new ApplicationListener<BeforeSaveEvent<IdentifiableEntity>>() {
         @Override
         public void onApplicationEvent(BeforeSaveEvent<IdentifiableEntity> event) {
            if (event.getEntity() == null) event.getEntity().setId(identifierFactory.generateId());
         }
      };
   }

      

I'm not sure if I can do this. I also tried to specify a specific one class

(for example BeforeSaveEvent<User>

) instead interface

, and I have the same problem, the event also captures other objects! = User

.

Can anyone clarify if it is possible what I am trying to do with spring-data-neo4j

(version 3.3.0.RELEASE

), and if so, an example of how to do it.

Thanks in advance.

+1


source to share





All Articles