我使用的是spring-boot应用框架,目前碰到2个问题。
1:缓存问题:目前是将hibernate作为Jpa的实现框架,cache的实现是ehcache,在application.properties里面配置了如下:
在最下一行,sharedCache处,现在是等于ALL,文档说是加入这个后,则不需要在entity加入@Cacheable就可以开启全局缓存,但是无论加还是没加(org.springframework.cache.annotation.Cacheable)或者是(javax.persistence.Cacheable;)都无效,我的调用方式是在junit中这样写的
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) public class ProductTest { @Inject private ProductService productService; @Test public void testOrder() throws Exception { System.out.println(productService.find_by_id(1)); } @Test public void testkk() throws Exception { System.out.println(productService.find_by_id(1)); System.out.println(productService.find_by_id(1)); System.out.println(productService.find_by_id(1)); } }但最后确发送了4条SQL语句,显然是没有缓存。同时缓存日志也没有用
hibernate.generate_statistics=true
问题2:延迟加载
在做ORM的时候,不管是1对多还是多对1,必须要使用
FetchType.EAGER
才不会出session closed的错误,可是我想用orm的二级缓存,就是想要出现N+1,但如果我使用这种配置,就会将子表一并查出,而不是我想要的单表的ID查询
可如果改成
FetchType.LAZY
就会出session closed的问题,网络上有那种在web.xml配置
OpenEntityManagerInViewFilter
可是,spring-boot没有web.xml,所以我就无从下手了,或者有其他的方法可以解决,谢谢各位了
@org.hibernate.annotations.Cache()
使用这个注解试试,如果你的controller使用了@Cacheable,那么OpenInViewFilter是没有用的,他对缓存结果没有控制,spring boot 默认是开启了这个filter,具体看jpa相关配置
######.请问下..楼主的问题解决了嘛?######
在WebAppInitializer重写
onStartup()方法 加上
FilterRegistration.Dynamic dynamic = servletContext.addFilter("openEntityManagerInViewFilter" ,org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.class);
dynamic.addMappingForUrlPatterns(null,false,"/*");
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。