Oracle 11g 的 Hibernate 4.3.6 配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25772426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate 4.3.6 Configuration with Oracle 11g
提问by Naveen
I am trying to configure Hibernate 4.3.6 with Oracle 11g, but i am not able to configure...I am
getting Null Pointer Exception while Creating the Session...i am putting all the configuration
and program file, kindly help me to get the root cause of the failure
我正在尝试使用 Oracle 11g 配置 Hibernate 4.3.6,但我无法配置......我
在创建会话时遇到空指针异常......我正在放置所有配置和程序文件,请帮助我找出失败的根本原因
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:XE</property>
<property name="username">TEST</property>
<property name="password">ORACLE</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.connection.pool_size">5</property>
</session-factory>
</hibernate-configuration>
HibernateUtil.java
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
public static SessionFactory createSessionFactory() {
try{
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
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;
}
}
FetchTest.java
public class FetchTest {
<br>
public static void main(String a[]){<br>
System.out.println("*********************** Inside Main ***********************");
<br><br>
Session session = HibernateUtil.getSessionFactory().openSession();
}
}
Output : -
*********************** Inside Main ***********************
Exception in thread "main" java.lang.NullPointerException
at com.naveen.org.FetchTest.main(FetchTest.java:18)
Please give your suggestions how to get ride from this.....?
回答by Chaitanya
NullPointerException
comes if you perform an operation on an object that is not instantiated.
NullPointerException
如果您对未实例化的对象执行操作,则会出现。
So you are getting this exception while doing:
因此,您在执行以下操作时会遇到此异常:
HibernateUtil.getSessionFactory().openSession();
which is nothing but:
这不过是:
sessionFactory.openSession();
As per the code you posted in your question the sessionFactory
should be null
because you created a static
variable like this:
根据您在问题中发布的代码,sessionFactory
应该是null
因为您创建了一个static
这样的变量:
private static SessionFactory sessionFactory;
and directly accessing it using method HibernateUtil.getSessionFactory()
without calling createSessionFactory()
anywhere in your code.
并使用方法直接访问它,HibernateUtil.getSessionFactory()
而无需createSessionFactory()
在代码中的任何地方调用。