如题 代码:
- Java code
package com.test.tree.client;import org.hibernate.cfg.Configuration;import org.hibernate.Session;import org.hibernate.SessionFactory;import com.test.pojo.Book;public class HibernateTest{ public static void main(String[] args) { Configuration cfg = new Configuration().configure(); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession();; session.beginTransaction(); Book book = new Book(); book.setBook_name("轻量级JavaEE企业应用实战"); book.setBook_editor("李刚"); book.setBook_pub("电子工业出版社"); book.setBook_price((float) 99.00); session.save(book); session.getTransaction().commit(); }}
错误代码:
Exception in thread "main" java.lang.NullPointerException
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:169)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2833)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2829)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
at com.test.tree.client.HibernateTest.main(HibernateTest.java:15)
为啥获取不到sessionfactory呢?
------解决方案--------------------------------------------------------
很有可能跟你的JAR包有关,看一下
------解决方案--------------------------------------------------------
很有可能是你的hibernate配置文件里面有错误
------解决方案--------------------------------------------------------
Configuration cfg = new Configuration();
SessionFactory sf =cfg.configure().buildSessionFactory();
这样试试看