Hibernate的二级缓存及jar包导入
来源:互联网 发布:四川大学锦江学院网络 编辑:程序博客网 时间:2024/05/09 20:52
测试Hibernate的二级缓存时,当启用二级缓存时,则会抛如下异常:
java.lang.NoClassDefFoundError: Could not initialize class com.hibernate.cache.util.HibernateUtilat com.hibernate.test.CacheTest.testLoad(CacheTest.java:28)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)at junit.framework.TestCase.runTest(TestCase.java:164)at junit.framework.TestCase.runBare(TestCase.java:130)at junit.framework.TestResult$1.protect(TestResult.java:106)at junit.framework.TestResult.runProtected(TestResult.java:124)at junit.framework.TestResult.run(TestResult.java:109)at junit.framework.TestCase.run(TestCase.java:120)at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
源码及jar包如下:
1、Student.java
import java.util.Date;public class Student {private int id;private String name;private Date createTime;//缺省get/set方法}
2、Student.hbm.xml
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping ><class name="com.hibernate.cache.entity.Student" table="t_student"><!--<cache usage="read-only"/>--><id name="id"><generator class="native"/></id><property name="name"/><property name="createTime"/></class></hibernate-mapping>
3、HibernateUtil.java
import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtil {private static SessionFactory sf = null;static{try{//读取hibernate.cfg.xml文件Configuration cfg = new Configuration().configure();//创建SessionFactorysf = cfg.buildSessionFactory();}catch(HibernateException e){e.printStackTrace();}}private HibernateUtil(){}public static SessionFactory getSessionFactory(){return sf;}public static Session getSession(){return sf.openSession();}public static void closeSession(Session session){if(session != null || session.isOpen()){session.close();}}}
4、hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">root</property><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><property name="hibernate.show_sql">true</property><!-- 配置缓存提供商 --><property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property><!-- 是否启用二级缓存 --><property name="hibernate.cache.use_second_level_cache">true</property><mapping resource="com/hibernate/cache/entity/Student.hbm.xml"/><!-- 指定student,使用二级缓存 --><class-cache class="com.hibernate.cache.entity.Student" usage="read-only"/></session-factory></hibernate-configuration>
5、CacheTest.java
import junit.framework.TestCase;import org.hibernate.HibernateException;import org.hibernate.Session;import com.hibernate.cache.entity.Student;import com.hibernate.cache.util.HibernateUtil;public class CacheTest extends TestCase {//二级缓存public void testLoad(){Session session = null;try {session = HibernateUtil.getSession();session.beginTransaction();Student stu = (Student)session.load(Student.class, 1);System.out.println(stu.getName());session.getTransaction().commit();} catch (HibernateException e) {session.getTransaction().rollback();e.printStackTrace();} finally {HibernateUtil.closeSession(session);}try {session = HibernateUtil.getSession();session.beginTransaction();Student stu = (Student)session.load(Student.class, 1);System.out.println(stu.getName());session.getTransaction().commit();} catch (HibernateException e) {session.getTransaction().rollback();e.printStackTrace();} finally {HibernateUtil.closeSession(session);}}}
6、ehcache.xml文件与log4j.properties文件是从下载的hibernate-distribution-3.3.2.GA-dist.zip中project\etc\下copy的,放入本地环境的源目录下
Hibernate的版本为:3.3.2
解决问题的方法:
缺少 commons-logging-1.1.1.jar jar包所造成的原因。
0 0
- Hibernate的二级缓存及jar包导入
- hibernate依赖的jar包及用途
- Hibernate二级缓存的配置及
- Jar包的导入
- 使用Hibernate需要导入的一些jar包
- 使用Hibernate需要导入的一些jar包
- 【转】使用Hibernate需要导入的一些jar包
- [转]使用Hibernate需要导入的一些JAR包
- [转]使用Hibernate需要导入的一些JAR包
- 使用Hibernate需要导入的一些JAR包
- 使用Hibernate需要导入的一些jar包
- [转]使用Hibernate需要导入的一些JAR包
- 使用Hibernate需要导入的一些JAR包
- hibernate入门第三课 导入必要的jar包
- Hibernate的jar包
- Hibernate的jar包
- Hibernate 的 jar 包
- Hibernate之必须导入jar包
- JS加入收藏和设为主页代码(兼容IE和FF)
- 在MySQL中,可以用 Limit 来查询第 m 列到第 n 列的记录
- 友盟渠道包工具
- DWR做的四级联动下拉菜单
- jqueryMobile中select样式自定义
- Hibernate的二级缓存及jar包导入
- LeetCode:Merge k Sorted Lists
- 数组和指针
- 全局对象Application的使用,以及如何在任何地方得到Application全局对象
- 命名空间“System.Web”中不存在类型或命名空间名称“Optimization”(是否缺少程序集引用?)
- MFC基本控件使用——静态文本框(Static)
- 数据库事务的特性ACID
- POJ 1004 Financial Management
- getopt()、getopt_long()用法变详解