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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制