是否可以在hibernate缓存中缓存延迟集合?
环境:Jboss wildfly,jpa 2,jpa提供者hibernate 4.3.1
persistence.xml包含:
<shared-cache-mode>ALL</shared-cache-mode>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true" />
实体:
@Entity
public class Actor extends BaseEntity implements NamedEntity, GatheredEntity {
@ManyToMany(mappedBy = "actors")
private Set<Movie> movies;
首次通过HQL查询工作进行访问。
[org.hibernate.engine.internal.StatisticalLoggingSessionEventListener] (default task-1) Session Metrics {
934038 nanoseconds spent acquiring 1 JDBC connections;
411968 nanoseconds spent releasing 1 JDBC connections;
2206400 nanoseconds spent preparing 11 JDBC statements;
26865079 nanoseconds spent executing 11 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
49744275 nanoseconds spent performing 182 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
784562 nanoseconds spent performing 10 L2C misses;
65398339 nanoseconds spent executing 1 flushes (flushing a total of 182 entities and 299 collections);
70728 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
}
第二次访问产生:
[org.hibernate.engine.internal.StatisticalLoggingSessionEventListener] (default task-10) Session Metrics {
0 nanoseconds spent acquiring 0 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
0 nanoseconds spent preparing 0 JDBC statements;
0 nanoseconds spent executing 0 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
3523604 nanoseconds spent performing 123 L2C hits;
84581 nanoseconds spent performing 4 L2C misses;
42442586 nanoseconds spent executing 1 flushes (flushing a total of 122 entities and 244 collections);
4740 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
}
缓存查询。但成员访问产生:
Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: model.Actor.movies, could not initialize proxy - no Session
这听起来成员“电影”没有缓存在l2c中,因为它是一个懒惰的初始化集合。
我尝试过注释:
@Entity
public class Actor extends BaseEntity implements NamedEntity, GatheredEntity {
@ManyToMany(mappedBy = "actors")
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
private Set<Movie> movies;
但是野生动物没有开始:
9:07:39,115 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014613: Operation ("deploy") failed - address: ([("deployment" => "moviedatabase.war")]) - failure description: {"JBAS014671: Failed services" => {"jboss.persistenceunit.\"moviedatabase.war#"" => "org.jboss.msc.service.StartException in service jboss.persistenceunit.\"moviedatabase.war": org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given; please either disable second level cache or set correct region factory using the hibernate.cache.region.factory_class setting and make sure the second level cache provider (hibernate-infinispan, e.g.) is available on the classpath.
Caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given; please either disable second level cache or set correct region factory using the hibernate.cache.region.factory_class setting and make sure the second level cache provider (hibernate-infinispan, e.g.) is available on the classpath."}}
我已经尝试了几个小时来为infispan设置hibernate.cache.region.factory_class一个野生动物。但我没有运气。 想要在l2c中缓存懒惰的集合,并通过hql访问。