在Eclipse环境里面,作了个简单的Hibernate例子,运行,但是报错:java.lang.ExceptionInInitializerError,具体的代码以及配置文件如下:
hibernate.cfg.xml配置内容:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">CBS </property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:CBSTJ
</property>
<property name="dialect">
net.sf.hibernate.dialect.OracleDialect
</property>
<property name="jdbc.fetch_size">50 </property>
<property name="jdbc.batch_size">25 </property>
<property name="show_sql">true </property>
<property name="use_outer_join">false </property>
<property name="connection.password">admin </property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<mapping resource="TRegister.hbm.xml"/>
</session-factory>
</hibernate-configuration>
TRegister.hbm.xml的内容:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="hibernate.PO.TRegister" table="t_register">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="userName" type="java.lang.String">
<column name="userName" length="30" />
</property>
<property name="userPwd" type="java.lang.String">
<column name="userPwd" length="30" />
</property>
<property name="sex" type="java.lang.String">
<column name="sex" length="10" />
</property>
<property name="age" type="java.lang.Integer">
<column name="age" />
</property>
</class>
</hibernate-mapping>
配置文件都放在classes下面。
HibernateUtil.java获取唯一Session实例:
package hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil{
private static final SessionFactory sessionFactory;
static
{
try
{
Configuration config = new Configuration().configure("/hibernate.cfg.xml");
sessionFactory = config.buildSessionFactory();
}
catch(Throwable e)
{ System.out.println("ExceptionInInitializerError----------" );
throw new ExceptionInInitializerError(e);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException
{
Session s = (Session)session.get();
//Open a new Session,if this Thread has none yet
if(s == null || !s.isOpen())
{
s = sessionFactory.openSession();