Hibernate核心xml + java
这里我尝试构建会话工厂对象,但获取NullPointerException。
HibernateUtil.java
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sessionFactory;
public static void initialize() {
try {
Configuration config = new Configuration().configure(HibernateUtil.class.getResource("/hibernate.cfg.xml"));
sessionFactory = config.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Error creating SessionFactory :" + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
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="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">postgres</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/protocol</property>
<property name="show_sql">true</property>
<mapping resource="in/nic/taphc/hc/protocol/persistence/State.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
有任何配置错误吗?请帮我弄清楚这个。