drooler: we currently use the Criteria class because that's how the original authors wanted to do it. i don't know know, using hql looks fine to me, but that's how they did it. i can try using hql to see if that helps.
question though, did you have to do anything more than add <cache usage="read-write"> to you class elements to get what you wanted? i am certainly wondering if my hbm files aren't properly setup.
jt_1000: well, i would think that caching the entity itself would have worked in my above examples considering i am looking up the object by id. in truth, i'm not sure i understand how caching entities versus collections is different at this point.
So how about this, lets reset things and try starting from the beginning because my current application was a hand me down and has quite a bit of code/config which may be clouding the issue. I tried creating a new test application so that I can figure this out and I still can't get any L2 caching to work in that app. I just want to work on a single object at first, then build from there. Here's what I have ...
Hibernate version: 3.0.5
Mapping documents:
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:comp/env/jdbc/test</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.cache.use_structured_entries">true</property>
<!-- Mapping files -->
<mapping resource="com/sun/wpe/test/Pojo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
<hibernate-mapping>
<class name="com.sun.wpe.test.Pojo" table="pojo">
<cache usage="read-write" />
<id name="field1" column="field1" type="java.lang.String" unsaved-value="undefined">
<generator class="assigned"/>
</id>
<property name="field2" type="java.lang.String"
update="true" insert="true" column="field2" unique="true"/>
<property name="field3" type="java.lang.String"
update="true" insert="true" column="field3" unique="true"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Transaction tx = session.beginTransaction();
Pojo pojo = (Pojo) session.load(Pojo.class, id);
tx.commit();
note, this is wrapped in a simple method called getPojo(id) which is called when i do my test.
Full stack trace of any exception that occurs:
Name and version of the database you are using:
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
INFO 2006-03-02 16:24:44,705 Environment:<clinit> - Hibernate 3.0.5
INFO 2006-03-02 16:24:44,711 Environment:<clinit> - hibernate.properties not found
INFO 2006-03-02 16:24:44,714 Environment:<clinit> - using CGLIB reflection optimizer
INFO 2006-03-02 16:24:44,719 Environment:<clinit> - using JDK 1.4 java.sql.Timestamp handling
INFO 2006-03-02 16:24:44,799 Configuration:configure - configuring from resource: /hibernate.cfg.xml
INFO 2006-03-02 16:24:44,801 Configuration:getConfigurationInputStream - Configuration resource: /hibernate.cfg.xml
DEBUG 2006-03-02 16:24:44,852 DTDEntityResolver:resolveEntity - trying to locate
http://hibernate.sourceforge.net/hibern ... on-3.0.dtd in classpath under org/hibernate/
DEBUG 2006-03-02 16:24:44,870 DTDEntityResolver:resolveEntity - found
http://hibernate.sourceforge.net/hibern ... on-3.0.dtd in classpath
DEBUG 2006-03-02 16:24:44,918 Configuration:addProperties - connection.datasource=java:comp/env/jdbc/test
DEBUG 2006-03-02 16:24:44,919 Configuration:addProperties - show_sql=true
DEBUG 2006-03-02 16:24:44,923 Configuration:addProperties - dialect=org.hibernate.dialect.MySQLDialect
DEBUG 2006-03-02 16:24:44,924 Configuration:addProperties - hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider
DEBUG 2006-03-02 16:24:44,925 Configuration:addProperties - hibernate.generate_statistics=true
DEBUG 2006-03-02 16:24:44,926 Configuration:addProperties - hibernate.cache.use_structured_entries=true
DEBUG 2006-03-02 16:24:44,928 Configuration:parseMappingElement - null<-org.dom4j.tree.DefaultAttribute@827968 [Attribute: name resource value "com/sun/wpe/test/Pojo.hbm.xml"]
INFO 2006-03-02 16:24:44,929 Configuration:addResource - Mapping resource: com/sun/wpe/test/Pojo.hbm.xml
DEBUG 2006-03-02 16:24:44,931 DTDEntityResolver:resolveEntity - trying to locate
http://hibernate.sourceforge.net/hibern ... ng-3.0.dtd in classpath under org/hibernate/
DEBUG 2006-03-02 16:24:44,935 DTDEntityResolver:resolveEntity - found
http://hibernate.sourceforge.net/hibern ... ng-3.0.dtd in classpath
INFO 2006-03-02 16:24:45,074 HbmBinder:bindRootPersistentClassCommonValues - Mapping class: com.sun.wpe.test.Pojo -> pojo
DEBUG 2006-03-02 16:24:45,082 HbmBinder:bindProperty - Mapped property: field1 -> field1
DEBUG 2006-03-02 16:24:45,097 HbmBinder:bindProperty - Mapped property: field2 -> field2
DEBUG 2006-03-02 16:24:45,099 HbmBinder:bindProperty - Mapped property: field3 -> field3
INFO 2006-03-02 16:24:45,100 Configuration:doConfigure - Configured SessionFactory: null
DEBUG 2006-03-02 16:24:45,104 Configuration:buildSessionFactory - Preparing to build session factory with filters : {}
INFO 2006-03-02 16:24:45,160 Configuration:secondPassCompile - processing extends queue
INFO 2006-03-02 16:24:45,170 Configuration:secondPassCompile - processing collection mappings
INFO 2006-03-02 16:24:45,171 Configuration:secondPassCompile - processing association property references
INFO 2006-03-02 16:24:45,172 Configuration:secondPassCompile - processing foreign key constraints
INFO 2006-03-02 16:24:45,318 NamingHelper:getInitialContext - JNDI InitialContext properties:{}
INFO 2006-03-02 16:24:45,338 DatasourceConnectionProvider:configure - Using datasource: java:comp/env/jdbc/test
INFO 2006-03-02 16:24:45,566 SettingsFactory:buildSettings - RDBMS: MySQL, version: 4.1.10-standard
INFO 2006-03-02 16:24:45,568 SettingsFactory:buildSettings - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.0.14-production ( $Date: 2004/04/24 15:49:43 $, $Revision: 1.27.2.39 $ )
INFO 2006-03-02 16:24:45,597 Dialect:<init> - Using dialect: org.hibernate.dialect.MySQLDialect
INFO 2006-03-02 16:24:45,609 TransactionFactoryFactory:buildTransactionFactory - Using default transaction strategy (direct JDBC transactions)
INFO 2006-03-02 16:24:45,613 TransactionManagerLookupFactory:getTransactionManagerLookup - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
INFO 2006-03-02 16:24:45,625 SettingsFactory:buildSettings - Automatic flush during beforeCompletion(): disabled
INFO 2006-03-02 16:24:45,626 SettingsFactory:buildSettings - Automatic session close at end of transaction: disabled
INFO 2006-03-02 16:24:45,627 SettingsFactory:buildSettings - JDBC batch size: 15
INFO 2006-03-02 16:24:45,628 SettingsFactory:buildSettings - JDBC batch updates for versioned data: disabled
INFO 2006-03-02 16:24:45,632 SettingsFactory:buildSettings - Scrollable result sets: enabled
DEBUG 2006-03-02 16:24:45,632 SettingsFactory:buildSettings - Wrap result sets: disabled
INFO 2006-03-02 16:24:45,633 SettingsFactory:buildSettings - JDBC3 getGeneratedKeys(): enabled
INFO 2006-03-02 16:24:45,634 SettingsFactory:buildSettings - Connection release mode: null
INFO 2006-03-02 16:24:45,636 SettingsFactory:buildSettings - Maximum outer join fetch depth: 2
INFO 2006-03-02 16:24:45,637 SettingsFactory:buildSettings - Default batch fetch size: 1
INFO 2006-03-02 16:24:45,639 SettingsFactory:buildSettings - Generate SQL with comments: disabled
INFO 2006-03-02 16:24:45,640 SettingsFactory:buildSettings - Order SQL updates by primary key: disabled
INFO 2006-03-02 16:24:45,641 SettingsFactory:createQueryTranslatorFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
INFO 2006-03-02 16:24:45,645 ASTQueryTranslatorFactory:<init> - Using ASTQueryTranslatorFactory
INFO 2006-03-02 16:24:45,646 SettingsFactory:buildSettings - Query language substitutions: {}
INFO 2006-03-02 16:24:45,646 SettingsFactory:buildSettings - Second-level cache: enabled
INFO 2006-03-02 16:24:45,649 SettingsFactory:buildSettings - Query cache: disabled
INFO 2006-03-02 16:24:45,650 SettingsFactory:createCacheProvider - Cache provider: org.hibernate.cache.HashtableCacheProvider
INFO 2006-03-02 16:24:45,652 SettingsFactory:buildSettings - Optimize cache for minimal puts: disabled
INFO 2006-03-02 16:24:45,653 SettingsFactory:buildSettings - Structured second-level cache entries: enabled
DEBUG 2006-03-02 16:24:45,655 SQLExceptionConverterFactory:buildSQLExceptionConverter - Using dialect defined converter
INFO 2006-03-02 16:24:45,673 SettingsFactory:buildSettings - Echoing all SQL to stdoutINFO 2006-03-02 16:24:45,674 SettingsFactory:buildSettings - Statistics: enabled
INFO 2006-03-02 16:24:45,675 SettingsFactory:buildSettings - Deleted entity synthetic identifier rollback: disabled
INFO 2006-03-02 16:24:45,677 SettingsFactory:buildSettings - Default entity-mode: pojoINFO 2006-03-02 16:24:45,826 SessionFactoryImpl:<init> - building session factory
DEBUG 2006-03-02 16:24:45,830 SessionFactoryImpl:<init> - Session factory constructed with filter configurations : {}
DEBUG 2006-03-02 16:24:45,864 CacheFactory:createCache - instantiating cache region: com.sun.wpe.test.Pojo usage strategy: read-write
DEBUG 2006-03-02 16:24:46,246 BasicEntityPersister:logStaticSQL - Static SQL for entity: com.sun.wpe.test.Pojo
DEBUG 2006-03-02 16:24:46,247 BasicEntityPersister:logStaticSQL - Version select: select field1 from pojo where field1 =?
DEBUG 2006-03-02 16:24:46,251 BasicEntityPersister:logStaticSQL - Snapshot select: select pojo_.field1, pojo_.field2 as field2_0_, pojo_.field3 as field3_0_ from pojo pojo_ where pojo_.field1=?
DEBUG 2006-03-02 16:24:46,252 BasicEntityPersister:logStaticSQL - Insert 0: insert into pojo (field2, field3, field1) values (?, ?, ?)
DEBUG 2006-03-02 16:24:46,253 BasicEntityPersister:logStaticSQL - Update 0: update pojo set field2=?, field3=? where field1=?
DEBUG 2006-03-02 16:24:46,254 BasicEntityPersister:logStaticSQL - Delete 0: delete from pojo where field1=?
DEBUG 2006-03-02 16:24:46,279 EntityLoader:<init> - Static select for entity com.sun.wpe.test.Pojo: select pojo0_.field1 as field1_0_, pojo0_.field2 as field2_0_0_, pojo0_.field3 as field3_0_0_ from pojo pojo0_ where pojo0_.field1=?
DEBUG 2006-03-02 16:24:46,280 EntityLoader:<init> - Static select for entity com.sun.wpe.test.Pojo: select pojo0_.field1 as field1_0_, pojo0_.field2 as field2_0_0_, pojo0_.field3 as field3_0_0_ from pojo pojo0_ where pojo0_.field1=?
DEBUG 2006-03-02 16:24:46,281 EntityLoader:<init> - Static select for entity com.sun.wpe.test.Pojo: select pojo0_.field1 as field1_0_, pojo0_.field2 as field2_0_0_, pojo0_.field3 as field3_0_0_ from pojo pojo0_ where pojo0_.field1=? for update
DEBUG 2006-03-02 16:24:46,282 EntityLoader:<init> - Static select for entity com.sun.wpe.test.Pojo: select pojo0_.field1 as field1_0_, pojo0_.field2 as field2_0_0_, pojo0_.field3 as field3_0_0_ from pojo pojo0_ where pojo0_.field1=? for update
DEBUG 2006-03-02 16:24:46,287 SessionFactoryObjectFactory:<clinit> - initializing class SessionFactoryObjectFactory
DEBUG 2006-03-02 16:24:46,309 SessionFactoryObjectFactory:addInstance - registered: 0112f2dd09bd7e030109bd7e05cd0000 (unnamed)
INFO 2006-03-02 16:24:46,310 SessionFactoryObjectFactory:addInstance - Not binding factory to JNDI, no JNDI name configured
DEBUG 2006-03-02 16:24:46,311 SessionFactoryImpl:<init> - instantiated session factory
INFO 2006-03-02 16:24:46,312 SessionFactoryImpl:checkNamedQueries - Checking 0 named queries
DEBUG 2006-03-02 16:33:18,306 SessionImpl:<init> - opened session at timestamp: 4674953209057280
DEBUG 2006-03-02 16:33:18,307 JDBCTransaction:begin - begin
DEBUG 2006-03-02 16:33:18,307 ConnectionManager:openConnection - opening JDBC connection
DEBUG 2006-03-02 16:33:18,308 JDBCTransaction:begin - current autocommit status: true
DEBUG 2006-03-02 16:33:18,312 JDBCTransaction:begin - disabling autocommit
DEBUG 2006-03-02 16:33:18,313 DefaultLoadEventListener:proxyOrLoad - loading entity: [com.sun.wpe.test.Pojo#abc]
DEBUG 2006-03-02 16:33:18,314 DefaultLoadEventListener:createProxyIfNecessary - creating new proxy for entity
DEBUG 2006-03-02 16:33:18,315 JDBCTransaction:commit - commit
DEBUG 2006-03-02 16:33:18,316 SessionImpl:managedFlush - automatically flushing sessionDEBUG 2006-03-02 16:33:18,317 JDBCContext:beforeTransactionCompletion - before transaction completion
DEBUG 2006-03-02 16:33:18,320 SessionImpl:beforeTransactionCompletion - before transaction completion
DEBUG 2006-03-02 16:33:18,321 JDBCTransaction:toggleAutoCommit - re-enabling autocommitDEBUG 2006-03-02 16:33:18,322 JDBCTransaction:commit - committed JDBC Connection
DEBUG 2006-03-02 16:33:18,322 JDBCContext:afterTransactionCompletion - after transaction completion
DEBUG 2006-03-02 16:33:18,323 SessionImpl:afterTransactionCompletion - after transaction completion
DEBUG 2006-03-02 16:33:18,324 SessionImpl:close - closing session
DEBUG 2006-03-02 16:33:18,325 ConnectionManager:closeConnection - closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
DEBUG 2006-03-02 16:33:18,328 JDBCContext:afterTransactionCompletion - after transaction completion
DEBUG 2006-03-02 16:33:18,329 SessionImpl:afterTransactionCompletion - after transaction completion
this application is dead simple. it contains only only a few classes, and just 1 object which i am trying to retrieve. so why isn't the caching working?