初学hibernate,配置完后运行出现
1、hibernate.dialect' must be set when no Connection available错误;
检查配置文件,写的很对就是不知道哪出错了,后来度娘告诉我:
Configuration cfg = new Configuration();这样写默认读取的是hibernate.properties配置文件,而我只是添加了,hibernate.cfg.xml配置文件,所以应这么写:
Configuration cfg = new Configuration().configure();
2、hibernate二级缓存配置。
hibernate二级缓存是由SessionFactory管理,所以又叫SessionFactory级缓存,它是通过不同的类库来实现 的,比如ehcache、oscache等。和一级缓存一样,二级缓存也是用来缓存实体对象的,对普通属性不缓存。
hibernate二级缓存的使用需要进行必要的配置,主要是四个地方(这里以ehcache为例):
1>. 配置echcache.xml文件
2>.开启二级缓存,修改hibernate.cfg.xml文件
true
3>.指定缓存产品提供商,修改hibernate.cfg.xml文件
org.hibernate.cache.EhCacheProvider
4>.指定那些实体类使用二级缓存(两种方法)
1).在映射文件中采用标签
2).在hibernate.cfg.xml文件中,采用标签
hibernate二级缓存配置上之后,就成了“客观存在”,hibernate在使用某些方法的时候默认就使用和维护了二级缓存(哪怕你出于 某种原因希望使用也不行)。因此,在使用二级缓存时进行一定的控制还是必要的,Session就提供了设置使用二级缓存的模式的方法 (setCacheMode)来实现,当session调用某个方法时对二级缓存的存取改变。
1.实体类:
Student.java
public class Student { private Integer id; private String name; //一系列的setter.getter方法 }
2.映射文件:
Student.hbm.xml
<class name="com.sxt.hibernate.cache.entity.Student" table="sxt_hibernate_student"> <!-- 指定本类的对象使用二级缓存(这也可以放在hibernate.cfg.xml中统一指 定) --> <!-- <cache usage="read-only"/> --> <id name="id" length="4"> <generator class="native"></generator> </id> <property name="name" length="10"></property> </class>
3. 二级缓存配置文件:
ehcache.xml
<ehcache> <!-- 当二级缓存溢出时,对象保存的临时磁盘路径 --> <diskStore path="java.io.tmpdir"/> <!--name="sampleCache2" 缓存名字 maxElementsInMemory="1000" 缓 存里可存放的最大对象数. eternal="true" 缓存对象是否永久有效(true表示是). timeToIdleSeconds="120" 对 象在缓存中存活的空闲时间,即空闲多久它就失效,单位是秒. timeToLiveSeconds="120" 对 象在缓存中存活的时间,单位是秒. overflowToDisk="true" 当缓存溢出时,对象 是否保存到磁盘上.保存的磁盘路径由<diskStore>中的path指定. --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /> </ehcache>
4.hibernate配置文件
hibernate.cfg.xml
<hibernate-configuration> <session-factory> <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ORCL10</property> <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="hibernate.connection.username">scott</property> <property name="hibernate.connection.password">yf123</property> <property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property> <property name="hibernate.show_sql">true</property> <!-- 开启二级缓存,其实hibernate默认就是开启的,这里显示的指定一下 --> <property name="hibernate.cache.use_second_level_cache">true</property> <!-- 指定二级缓存产品的提供商 --> <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <mapping resource="com/sxt/hibernate/cache/entity/Student.hbm.xml"/> <!-- 指定那些类使用二级缓存 --> <class-cache usage="read-only" class="com.sxt.hibernate.cache.entity.Student"/> </session-factory> </hibernate-configuration>
在映射文件中配置:<cache usage="read-only"/>
注意:在这有4个值:有必要对这4个值进行介绍。
A.transactional:保存可重复的事物隔离级别,对于读/写比例大,很少更新的数据通常可以采用这种方式。
B.read-write:使用timestamp机制维护已提交的事物隔离级别,对于读/写比例大,很少更新的数据通常可以采用这种方式。
C.nonstrict-read-write:二级缓存与数据库中的数据可能会出现不一致的情况。应该设置足够短的缓存过期时间。否则就有可能从缓存中读取到脏数据,当一些数据很少改变(一天,两天都不改变的数据)并且这些数据如果出现数据库与缓存不一致的情况下影响并不大的时候,那么可以采用这种缓存策略。
D.read-only:当确定数据不会被更改时。我们可以使用这种缓存策略。