@Preupdate and @Prepersist in Hibernate/JPA (Using Session)

@PreUpdate and @Prepersist in hibernate/JPA (using session)

Now it seems that those interpreters are not been executed, with little search i found out that it's suitable using entityManager.

Yes, the JPA callbacks won't work if you're using the Session API.

So I'll like to know if is there a SIMPLE way around my problem, meaning having @PrePersist or @PreUpdate or even other workaround to set the lastModified field still using session

To my knowledge, there is no simple way around (if you're using Spring, MAYBE have a look at this post though).

My suggestion for an Hibernate based solution would be to use events (and one or more interface(s)). Check Hibernate and last modified date for an example.

JPA PrePersist and PreUpdate

As Alan Hay said, an em.flush() right before the persist works just fine in this case. It's not a duplicate of the question suggested, because flushing works.

Hibernate and JPA @PreUpdate and @PrePersist not working

I am guessing that entities that are inserted or updated using native SQL or JPQL in this way will by pass the persistent context and persistence context will not manage them. However , @PreUpdate and @PrePersist only work for the entities that are managed by persistence context , so your @PreUpdate and @PrePersist will not execute for them.

I think you should insert and update the entities in a more JPA way which ensure persistence context will manage them:

@Service
public class FileManagementService{

@Autowired
private FileManagementRepository fileManagementRepository;

@Transactional
public void updateFileWithFilename(String id, String name) {
Optional<FileManagement> file= fileManagementRepository.findById(id);
if(file.isPresent()){
file.get().setName(name);
}else{
throw new RuntimeException("Record does not exist");
}
}

@Transactional
public void createFileWithFilename(String name, String filename, String createdBy, Date createdDate, boolean isActive, boolean isDeleted) {
FileManagement file= new FileManagement(name,fileName,........);
fileManagementRepository.save(file);
}
}

JPA - EntityListener - @PrePersist and @PreUpdate work successfully, but @PostPersist and @PostUpdate not working

You set updatable = false on dataRegisto field so it seems normal the setter doesn't update the value in database.

@PostUpdate is called after fulshing the session.
Could you try to add :

@Autowired
private EntityManager entityManager;

And after calling update :

entityManager.flush();

Best regards.

Does @PreUpdate always run when @PrePersist runs?

No, @PreUpdate callback method does not always run when @PrePersist callback method is executed. @PrePersist is executed before persist operation (direct or cascaded) and @PreUpdate before database update.

In JPA 2.1 specification (3.5.3 Semantics of the Life Cycle Callback Methods for Entities) this is told with following words:

The PrePersist and PreRemove callback methods are invoked for a given
entity before the respective EntityManager persist and remove
operations for that entity are executed.

...

The PreUpdate and
PostUpdate callbacks occur before and after the database update
operations to entity data respectively.

JPA / Hibernate question ( @PrePersist, Session API clarification)

What is the main prove that Session API is used in the app

Oversimplifying, the EntityManager API is a "shell" around the original Session API. But I think you want another answer :-) The only other answer I can give is to check the source code. Or, if you don't have the source code, you'll want to change the logging configuration. Set it to "trace" for the Session class, then you can see exactly when it's being called.

and is there a chance to retrieve directly EntityManager so the triggers are fired ?

Sure, depending on the application server or framework you are using. If you have a "real" application server, you can retrieve the EM from it. If you are using a standalone application, you can use the EntityManagerFactory to create a EntityManager. But before doing that, I would check the examples in the Hibernate test suite:

https://github.com/hibernate/hibernate-core/blob/master/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/LastUpdateListener.java

Also, if you are not using the JPA API, you can still have event listeners in Hibernate:

http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#objectstate-events

@PrePersist insert query

As you mentioned there are some problems with this implementation. Some of them are:

  • EntityManager instantiation
  • Transaction handling Synchronous and slow
  • persist operation inside the handler.

I believe the best way to do that is to separate MySQL and MongoDB persistence completely in diferent DAO/Repositories and coordinate this from an upper layer or interceptor. The implementation will be diferent depending of with platform is being used (Java SE/EE/Spring).

I guess you don't need MongoDB at all. Removing MongoDB would make the solution simpler. You could handle the entity size problem inside MySQL using a custom tablespace or another DB level trick.



Related Topics



Leave a reply



Submit