ehcache没有使用hibernate

时间:2018-03-08 12:15:33

标签: java hibernate ehcache dropwizard

我有一个dropwizard应用程序,在数据库的config.yml中配置。

属性是:

hibernate.show_sql: true
javax.persistence.sharedCache.mode: ALL
hibernate.cache.default_cache_concurrency_strategy: read-only
hibernate.cache.region.factory_class: 
org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
hibernate.cache.use_second_level_cache: true

但似乎ehcache不能正常工作......

在日志中,我总是看到查询和ehcache日志:

Hibernate: select businessel0_.GD_BUSINESSCODEELEMENT as GD_BUSINESSCODEELE1_3_0_, businessel0_.DBUSERINS as DBUSERINS2_3_0_, businessel0_.DBUSERUPD as DBUSERUPD3_3_0_, businessel0_.INSERTDATE as INSERTDATE4_3_0_, businessel0_.MODIFYDATE as MODIFYDATE5_3_0_, businessel0_.AU_USERIDINS as AU_USERIDINS10_3_0_, businessel0_.AU_USERIDUPD as AU_USERIDUPD11_3_0_, businessel0_.VERSION as VERSION6_3_0_, businessel0_.GD_BUSINESSCODETYPE as GD_BUSINESSCODETY12_3_0_, businessel0_.DATATYPE as DATATYPE7_3_0_, businessel0_.DESCRIPTION as DESCRIPTION8_3_0_, businessel0_.LONGDESC as LONGDESC9_3_0_, businessel0_.MASTER_BUSINESSCODEELEMENT as MASTER_BUSINESSCO13_3_0_, businessty1_.GD_BUSINESSCODETYPE as GD_BUSINESSCODETYP1_5_1_, businessty1_.DBUSERINS as DBUSERINS2_5_1_, businessty1_.DBUSERUPD as DBUSERUPD3_5_1_, businessty1_.INSERTDATE as INSERTDATE4_5_1_, businessty1_.MODIFYDATE as MODIFYDATE5_5_1_, businessty1_.AU_USERIDINS as AU_USERIDINS8_5_1_, businessty1_.AU_USERIDUPD as AU_USERIDUPD9_5_1_, businessty1_.VERSION as VERSION6_5_1_, businessty1_.DESCRIPTION as DESCRIPTION7_5_1_, businessel2_.GD_BUSINESSCODEELEMENT as GD_BUSINESSCODEELE1_3_2_, businessel2_.DBUSERINS as DBUSERINS2_3_2_, businessel2_.DBUSERUPD as DBUSERUPD3_3_2_, businessel2_.INSERTDATE as INSERTDATE4_3_2_, businessel2_.MODIFYDATE as MODIFYDATE5_3_2_, businessel2_.AU_USERIDINS as AU_USERIDINS10_3_2_, businessel2_.AU_USERIDUPD as AU_USERIDUPD11_3_2_, businessel2_.VERSION as VERSION6_3_2_, businessel2_.GD_BUSINESSCODETYPE as GD_BUSINESSCODETY12_3_2_, businessel2_.DATATYPE as DATATYPE7_3_2_, businessel2_.DESCRIPTION as DESCRIPTION8_3_2_, businessel2_.LONGDESC as LONGDESC9_3_2_, businessel2_.MASTER_BUSINESSCODEELEMENT as MASTER_BUSINESSCO13_3_2_ from GD_BUSINESSCODEELEMENT businessel0_ left outer join GD_BUSINESSCODETYPE businessty1_ on businessel0_.GD_BUSINESSCODETYPE=businessty1_.GD_BUSINESSCODETYPE left outer join GD_BUSINESSCODEELEMENT businessel2_ on businessel0_.MASTER_BUSINESSCODEELEMENT=businessel2_.GD_BUSINESSCODEELEMENT where businessel0_.GD_BUSINESSCODEELEMENT=?
2018-03-08 13:11:16,243 DEBUG 99FE394CE00B497699A5A9916B1E9A6A [net.sf.ehcache.store.disk.Segment]: put added 0 on heap
2018-03-08 13:11:16,247 DEBUG 99FE394CE00B497699A5A9916B1E9A6A [net.sf.ehcache.store.disk.Segment]: put added 0 on heap
Hibernate: select businessel0_.GD_BUSINESSCODEELEMENT as GD_BUSINESSCODEELE1_3_0_, businessel0_.DBUSERINS as DBUSERINS2_3_0_, businessel0_.DBUSERUPD as DBUSERUPD3_3_0_, businessel0_.INSERTDATE as INSERTDATE4_3_0_, businessel0_.MODIFYDATE as MODIFYDATE5_3_0_, businessel0_.AU_USERIDINS as AU_USERIDINS10_3_0_, businessel0_.AU_USERIDUPD as AU_USERIDUPD11_3_0_, businessel0_.VERSION as VERSION6_3_0_, businessel0_.GD_BUSINESSCODETYPE as GD_BUSINESSCODETY12_3_0_, businessel0_.DATATYPE as DATATYPE7_3_0_, businessel0_.DESCRIPTION as DESCRIPTION8_3_0_, businessel0_.LONGDESC as LONGDESC9_3_0_, businessel0_.MASTER_BUSINESSCODEELEMENT as MASTER_BUSINESSCO13_3_0_, businessty1_.GD_BUSINESSCODETYPE as GD_BUSINESSCODETYP1_5_1_, businessty1_.DBUSERINS as DBUSERINS2_5_1_, businessty1_.DBUSERUPD as DBUSERUPD3_5_1_, businessty1_.INSERTDATE as INSERTDATE4_5_1_, businessty1_.MODIFYDATE as MODIFYDATE5_5_1_, businessty1_.AU_USERIDINS as AU_USERIDINS8_5_1_, businessty1_.AU_USERIDUPD as AU_USERIDUPD9_5_1_, businessty1_.VERSION as VERSION6_5_1_, businessty1_.DESCRIPTION as DESCRIPTION7_5_1_, businessel2_.GD_BUSINESSCODEELEMENT as GD_BUSINESSCODEELE1_3_2_, businessel2_.DBUSERINS as DBUSERINS2_3_2_, businessel2_.DBUSERUPD as DBUSERUPD3_3_2_, businessel2_.INSERTDATE as INSERTDATE4_3_2_, businessel2_.MODIFYDATE as MODIFYDATE5_3_2_, businessel2_.AU_USERIDINS as AU_USERIDINS10_3_2_, businessel2_.AU_USERIDUPD as AU_USERIDUPD11_3_2_, businessel2_.VERSION as VERSION6_3_2_, businessel2_.GD_BUSINESSCODETYPE as GD_BUSINESSCODETY12_3_2_, businessel2_.DATATYPE as DATATYPE7_3_2_, businessel2_.DESCRIPTION as DESCRIPTION8_3_2_, businessel2_.LONGDESC as LONGDESC9_3_2_, businessel2_.MASTER_BUSINESSCODEELEMENT as MASTER_BUSINESSCO13_3_2_ from GD_BUSINESSCODEELEMENT businessel0_ left outer join GD_BUSINESSCODETYPE businessty1_ on businessel0_.GD_BUSINESSCODETYPE=businessty1_.GD_BUSINESSCODETYPE left outer join GD_BUSINESSCODEELEMENT businessel2_ on businessel0_.MASTER_BUSINESSCODEELEMENT=businessel2_.GD_BUSINESSCODEELEMENT where businessel0_.GD_BUSINESSCODEELEMENT=?
2018-03-08 13:11:16,252 DEBUG 99FE394CE00B497699A5A9916B1E9A6A [net.sf.ehcache.store.disk.Segment]: put added 0 on heap
Hibernate: select businessel0_.GD_BUSINESSCODEELEMENT as GD_BUSINESSCODEELE1_3_0_, businessel0_.DBUSERINS as DBUSERINS2_3_0_, businessel0_.DBUSERUPD as DBUSERUPD3_3_0_, businessel0_.INSERTDATE as INSERTDATE4_3_0_, businessel0_.MODIFYDATE as MODIFYDATE5_3_0_, businessel0_.AU_USERIDINS as AU_USERIDINS10_3_0_, businessel0_.AU_USERIDUPD as AU_USERIDUPD11_3_0_, businessel0_.VERSION as VERSION6_3_0_, businessel0_.GD_BUSINESSCODETYPE as GD_BUSINESSCODETY12_3_0_, businessel0_.DATATYPE as DATATYPE7_3_0_, businessel0_.DESCRIPTION as DESCRIPTION8_3_0_, businessel0_.LONGDESC as LONGDESC9_3_0_, businessel0_.MASTER_BUSINESSCODEELEMENT as MASTER_BUSINESSCO13_3_0_, businessty1_.GD_BUSINESSCODETYPE as GD_BUSINESSCODETYP1_5_1_, businessty1_.DBUSERINS as DBUSERINS2_5_1_, businessty1_.DBUSERUPD as DBUSERUPD3_5_1_, businessty1_.INSERTDATE as INSERTDATE4_5_1_, businessty1_.MODIFYDATE as MODIFYDATE5_5_1_, businessty1_.AU_USERIDINS as AU_USERIDINS8_5_1_, businessty1_.AU_USERIDUPD as AU_USERIDUPD9_5_1_, businessty1_.VERSION as VERSION6_5_1_, businessty1_.DESCRIPTION as DESCRIPTION7_5_1_, businessel2_.GD_BUSINESSCODEELEMENT as GD_BUSINESSCODEELE1_3_2_, businessel2_.DBUSERINS as DBUSERINS2_3_2_, businessel2_.DBUSERUPD as DBUSERUPD3_3_2_, businessel2_.INSERTDATE as INSERTDATE4_3_2_, businessel2_.MODIFYDATE as MODIFYDATE5_3_2_, businessel2_.AU_USERIDINS as AU_USERIDINS10_3_2_, businessel2_.AU_USERIDUPD as AU_USERIDUPD11_3_2_, businessel2_.VERSION as VERSION6_3_2_, businessel2_.GD_BUSINESSCODETYPE as GD_BUSINESSCODETY12_3_2_, businessel2_.DATATYPE as DATATYPE7_3_2_, businessel2_.DESCRIPTION as DESCRIPTION8_3_2_, businessel2_.LONGDESC as LONGDESC9_3_2_, businessel2_.MASTER_BUSINESSCODEELEMENT as MASTER_BUSINESSCO13_3_2_ from GD_BUSINESSCODEELEMENT businessel0_ left outer join GD_BUSINESSCODETYPE businessty1_ on businessel0_.GD_BUSINESSCODETYPE=businessty1_.GD_BUSINESSCODETYPE left outer join GD_BUSINESSCODEELEMENT businessel2_ on businessel0_.MASTER_BUSINESSCODEELEMENT=businessel2_.GD_BUSINESSCODEELEMENT where businessel0_.GD_BUSINESSCODEELEMENT=?
2018-03-08 13:11:16,256 DEBUG 99FE394CE00B497699A5A9916B1E9A6A [net.sf.ehcache.store.disk.Segment]: put added 0 on heap
Hibernate: select statuscode0_.GD_STATUS as GD_STATUS1_15_0_, statuscode0_.DBUSERINS as DBUSERINS2_15_0_, statuscode0_.DBUSERUPD as DBUSERUPD3_15_0_, statuscode0_.INSERTDATE as INSERTDATE4_15_0_, statuscode0_.MODIFYDATE as MODIFYDATE5_15_0_, statuscode0_.AU_USERIDINS as AU_USERIDINS8_15_0_, statuscode0_.AU_USERIDUPD as AU_USERIDUPD9_15_0_, statuscode0_.VERSION as VERSION6_15_0_, statuscode0_.DESCRIPTION as DESCRIPTION7_15_0_ from GD_STATUSCODE statuscode0_ where statuscode0_.GD_STATUS=?
Hibernate: select statuscode0_.GD_STATUS as GD_STATUS1_15_0_, statuscode0_.DBUSERINS as DBUSERINS2_15_0_, statuscode0_.DBUSERUPD as DBUSERUPD3_15_0_, statuscode0_.INSERTDATE as INSERTDATE4_15_0_, statuscode0_.MODIFYDATE as MODIFYDATE5_15_0_, statuscode0_.AU_USERIDINS as AU_USERIDINS8_15_0_, statuscode0_.AU_USERIDUPD as AU_USERIDUPD9_15_0_, statuscode0_.VERSION as VERSION6_15_0_, statuscode0_.DESCRIPTION as DESCRIPTION7_15_0_ from GD_STATUSCODE statuscode0_ where statuscode0_.GD_STATUS=?
2018-03-08 13:11:16,263 DEBUG 99FE394CE00B497699A5A9916B1E9A6A [net.sf.ehcache.store.disk.Segment]: put added 0 on heap

DAO代码非常简单:

@DAO
public class ContractDAO extends AbstractDAO<Contract> {

    public ContractDAO(SessionFactory sessionFactory) {
        super(sessionFactory);
    }

    public List<Contract> getAll() {
        return list(query(" from Contract c"));
    }

    public Contract getById(int id) {
        return query(" from Contract c where c.id = "+id).getSingleResult();
    }
}

知道出了什么问题吗?

1 个答案:

答案 0 :(得分:0)

您需要将持久对象和查询配置为可缓存。

只需将@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)添加到Contract类即可缓存实体。

然后,Hibernate将从缓存而不是数据库中获取实体。但是,在您的情况下,您正在使用查询。我不是一个期望的人,但可能有一个findById可以取代query(" from Contract c where c.id = "+id)。顺便说一句,你不应该那样连接。而是在预准备语句中使用参数。

对于getAll,您需要将查询设置为可缓存。再一次,我不知道它是如何用dropwizard完成的,但某处必须有setCacheable