问题描述:

Basically I need to count the times an object gets persisted, updated, etc. With @PostPersist, @PostUpdate and so on.

But I also need to know how long these store-actions take (only altogether), is there a way to do it with the (default) Entity Listener?

网友答案:

You could activate the hibernate statistics: set hibernate.generate_statistics to true eigher by command line property by configuration in persistence.xml

...
<properties>
    ...
    <property name="hibernate.generate_statistics" value="true"/>   
<properties>

and add to your logging configuration

<logger name="org.hibernate.stat" level="DEBUG" />

This would tell you for each session how many SQL statments are executed and how long does it take

BTW: internally it is done by org.hibernate.engine.internal.StatisticalLoggingSessionEventListener

网友答案:

I believe the better way to achieve this is to use AOP, intercepting the methods you want to measure and doing anything you want. Spring AOP seems perfect for this.

I know it's not related with Entity Listener, but it's just a different ideia =)

Documentation with examples: http://docs.spring.io/autorepo/docs/spring-framework/3.2.x/spring-framework-reference/html/aop-api.html

相关阅读:
Top