嗨,我正在使用 Eclipse 这个休眠更新。
我有这个配置:
<?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">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">protein_tracker</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
</session-factory>
</hibernate-configuration>
我有这个类具有启动休眠的基础知识:
package com.simpleprogramer;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtilities {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
static{
try{
Configuration configuration = new Configuration().configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
catch(HibernateException exeption)
{
System.out.println("Problem creating session factory!");
}
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
和主要测试休眠会话,我不知道是空的:
package com.simpleprogramer;
import org.hibernate.Session;
public class program {
public static void main(String[] args) {
System.out.println("Hola mundo");
Session session = HibernateUtilities.getSessionFactory().openSession();
session.close();
}
}
从控制台收到此错误:
Hola mundo
oct 01, 2013 2:12:31 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
oct 01, 2013 2:12:31 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.6.Final}
oct 01, 2013 2:12:31 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
oct 01, 2013 2:12:31 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
oct 01, 2013 2:12:31 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
oct 01, 2013 2:12:31 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
oct 01, 2013 2:12:31 AM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
oct 01, 2013 2:12:31 AM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
oct 01, 2013 2:12:31 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Problem creating session factory!
Exception in thread "main" java.lang.NullPointerException
at com.simpleprogramer.program.main(program.java:9)
您好,谢谢您的建议 exeption.printStacktrace(System.out);。我发现我的 jdbc 驱动程序没有加载,然后意识到它根本没有在 proyect 上加载(我认为无论驱动程序是什么或是什么,配置都会自动加载)。所以我下载了相应的jar,它可以工作。谢谢你的建议!