Hibernate Configured SessionFactory: null 空指针 错误异常
控制台信息
6月 07, 2018 11:53:13 上午 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
6月 07, 2018 11:53:13 上午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.4.Final}
6月 07, 2018 11:53:13 上午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
6月 07, 2018 11:53:13 上午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
6月 07, 2018 11:53:13 上午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
6月 07, 2018 11:53:13 上午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.dom4j.io.SAXContentHandler (file:/D:/Program%20Files/eclipse-workspace/hibernate-1/lib/dom4j-1.6.1.jar) to method com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser$LocatorProxy.getEncoding()
WARNING: Please consider reporting this to the maintainers of org.dom4j.io.SAXContentHandler
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
6月 07, 2018 11:53:14 上午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/gdq/hibernate/helloworld/News.hbm.xml
6月 07, 2018 11:53:14 上午 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
6月 07, 2018 11:53:14 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
6月 07, 2018 11:53:14 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
6月 07, 2018 11:53:14 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
6月 07, 2018 11:53:14 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc.mysql://localhost:3306/hibernate4]
6月 07, 2018 11:53:14 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {password=****, user=root}
单元测试类
package com.gdq.hibernate.helloworld;
import java.sql.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.jupiter.api.Test;
class HibernateTest {
@Test
void test() {
//1.创建一个SessionFactory 对象
SessionFactory sessionFactory ;
//1)创建Configuration 对象: 对应hibernate的基本配置信息和对象关系映射信息
Configuration configuration = new Configuration().configure();
//2)创建一个ServiceRegistry 对象 hibernate 配置和服务 在该对象注册
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
//2.创建一个Session对象
Session session = sessionFactory.openSession();
//3.开启事务
Transaction transaction=session.beginTransaction();
//4.执行保存操作
News news = new News("Java","God_q1",new Date(new java.util.Date().getDate()));
System.out.println(news.toString());
session.save(news);
//5.提交事务
transaction.commit();
//6.关闭Session
session.close();
//7.关闭SessionFactory
sessionFactory.close();
}
}
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">
<hibernate-configuration>
<session-factory>
<!-- 配置数据库基本信息 -->
<property name="connection.username">root</property>
<property name="connection.password">7436</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc.mysql://localhost:3306/hibernate4</property>
<!-- 制定自动生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property>
<!-- 配置hibernate的基本信息 -->
<!-- hibernate 数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- 控制台打印SQL -->
<property name="show_sql">true</property>
<!-- SQL格式化 -->
<property name="format_sql">true</property>
<!-- 指定关联的.hbm.xml -->
<mapping resource="com/gdq/hibernate/helloworld/News.hbm.xml"/>
</session-factory>
</hibernate-configuration>
映射文件
<?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">
<hibernate-mapping>
<class name="com.gdq.hibernate.helloworld.News" table="NEWS">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="title" type="java.lang.String">
<column name="TITLE" />
</property>
<property name="author" type="java.lang.String">
<column name="AUTHOR" />
</property>
<property name="date" type="java.sql.Date">
<column name="DATE" />
</property>
</class>
</hibernate-mapping>