开发者

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

I am working curr开发者_运维技巧ently on creation of some tests on my app. I have faced an issue which needs to involve @PrePersist feature from JPA. Unfortunately those interceptors aren't fired, while persisting, updating ( etc ) and I found that with Session API of Hibernate this feature does not work. Here appears my question.

What is the main prove that Session API is used in the app, and is there a chance to retrieve directly EntityManager so the triggers are fired ?

I greatly appreciate any hints and advice on this topic,


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜