wonder_2025 2014-05-22 14:31
浏览 2519

第一次使用hibernate就出错

使用hibernate4报错
Initial SessionFactory creation failed.java.lang.NullPointerException
Exception in thread "main" java.lang.ExceptionInInitializerError
at HibernateUtil.buildSessionFactory(HibernateUtil.java:22)
at HibernateUtil.(HibernateUtil.java:9)
at TestStudent.main(TestStudent.java:22)
Caused by: java.lang.NullPointerException
at HibernateUtil.buildSessionFactory(HibernateUtil.java:16)
... 2 more

mysql—connector-java使用的版本是5.1.22
hibernate使用的版本是4.3.5

以下是程序:

TestStudent.java
import javax.imageio.spi.ServiceRegistry;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.bjsxt.hibernate.model.Student;
public class TestStudent {
public static void main(String args[]){
Student s=new Student();
s.setId(1);
s.setName("s1");
s.setAge(1);
SessionFactory sf=HibernateUtil.getSessionFactory();
Session session=sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
}

}

HibernateUtil.java
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static Configuration cfg=new Configuration();
private static SessionFactory buildSessionFactory() {
try {
return cfg.configure().buildSessionFactory(new StandardServiceRegistryBuilder().build());
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);

        throw new ExceptionInInitializerError(ex);

    }
}
public static SessionFactory getSessionFactory() {
    return sessionFactory;
}

}
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<session-factory>
    <property name="javax.persistence.validation.mode">none</property> 
    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
    <property name="connection.username">ru</property>
    <property name="connection.password">qq</property>

    <!-- JDBC connection pool (use the built-in) -->
    <!--<property name="connection.pool_size">1</property>-->

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

    <!-- Enable Hibernate's automatic session context management -->
    <!--<property name="current_session_context_class">thread</property>-->

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <!--<property name="hbm2ddl.auto">update</property>-->

   <mapping resource="com/bjsxt/hibernate/model/Student.hbm.xml"/>
 </session-factory>


student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">







  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 求数学坐标画圆以及直线的算法
    • ¥35 平滑拟合曲线该如何生成
    • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
    • ¥15 名为“Product”的列已属于此 DataTable
    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 自己瞎改改,结果现在又运行不了了
    • ¥15 链式存储应该如何解决