JPA中的二级缓存,使用实体管理器进行本机查询以及JPQL

时间:2018-01-17 05:44:15

标签: java mysql hibernate jpa caching

我已经通过EntityManager使用jpa并使用java配置实现了二级缓存。我提到了以下链接http://www.baeldung.com/hibernate-second-level-cache。我没有收到任何错误,但我的实体的数据也没有缓存。请帮助:)

我已经提供了在应用程序中启用L2缓存所需的所有配置,这是我的配置代码: -

这是我的application.properties文件 -

spring.jpa.properties.javax.persistence.sharedCache.mode = ENABLE_SELECTIVE
spring.jpa.properties.hibernate.cache.use_query_cache = true
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.provider_class = net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
spring.jpa.properties.hibernate.jdbc.batch_size=10

这是我的实体类 -

@Entity
@Cacheable(true)
@org.hibernate.annotations.Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
@Table(name = "booking")

这是我在我的应用程序的控制台日志中得到的,每次我遇到相同的请求时,查询在每次请求时都会访问数据库。这就是为什么没有参与L2缓存的原因。

Hibernate: SELECT booking.BId,booking.calledDate Called, booking.calleduser Caller, booking.callerNotes, booking.Title, booking.FName, booking.ScName, booking.bookingPhase, Training.TName, booking.TrDate, booking.SubmitDateTime, trlocation.TrLocation trTrl, booking.STATUS, booking.DelegNum, booking.Tel, booking.Email,booking.Notes FROM(SELECT booking.* FROM booking booking WHERE booking.SubmitDateTime > DATE_SUB(NOW(), INTERVAL 2 MONTH) AND booking.SId > 0 ORDER BY booking.BId DESC LIMIT 1) booking LEFT JOIN training Training ON booking.Training = Training.TId LEFT JOIN confirmation ON booking.Bid = confirmation.Bid LEFT JOIN trlocation ON booking.TrLocation = trlocation.TrLId LEFT JOIN invoice ON booking.BId =invoice.BId AND invoice.PayReceiveBy='' AND invoice.Id='-1' LEFT JOIN couriertracking ON booking.BId = couriertracking.bId LEFT JOIN salesperson ON booking.SId = salesperson.SId AND booking.SId > 0 
----------the data is NOT cached
Hibernate: SELECT booking.BId,booking.calledDate Called, booking.calleduser Caller, booking.callerNotes, booking.Title, booking.FName, booking.ScName, booking.bookingPhase, Training.TName, booking.TrDate, booking.SubmitDateTime, trlocation.TrLocation trTrl, booking.STATUS, booking.DelegNum, booking.Tel, booking.Email,booking.Notes FROM(SELECT booking.* FROM booking booking WHERE booking.SubmitDateTime > DATE_SUB(NOW(), INTERVAL 2 MONTH) AND booking.SId > 0 ORDER BY booking.BId DESC LIMIT 1) booking LEFT JOIN training Training ON booking.Training = Training.TId LEFT JOIN confirmation ON booking.Bid = confirmation.Bid LEFT JOIN trlocation ON booking.TrLocation = trlocation.TrLId LEFT JOIN invoice ON booking.BId =invoice.BId AND invoice.PayReceiveBy='' AND invoice.Id='-1' LEFT JOIN couriertracking ON booking.BId = couriertracking.bId LEFT JOIN salesperson ON booking.SId = salesperson.SId AND booking.SId > 0 
----------the data is NOT cached

0 个答案:

没有答案