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
java spring-data spring-data-neo4j neo4j


source to share


No one has answered this question yet

See similar questions:

2
Use @NodeEntity for interface / abstract class
1
Does spring-data-neo4j have a beforeSave method?

or similar:

1187
Is null check required before calling instanceof?
40
Wrong ordering in generated table in jpa
4
Can't use enum in repository request (neo4j / Spring Data)
2
@RepositoryEventHandler events not called for Neo4j repositories
1
Neo4j SDN preopulate entity
1
Neo4j TimeTree in Spring Data from Neo4j 4.0
1
NodeBacked entity NPE - entityState is null
0
SDN relationship is not removed
0
The uniqueness of the SDN relationship
0
How do I update a property of a relation in spring-data-neo4j?



All Articles
Loading...
X
Show
Funny
Dev
Pics