简体   繁体   English

创建 sessionfactory 时空指针异常休眠

[英]null pointer exception hibernate on creating sessionfactory

Hi i trying to write my first Hibernate program, its giving an error while instantiating the session factory.嗨,我正在尝试编写我的第一个 Hibernate 程序,它在实例化会话工厂时出错。 I'm using hibernate 5.0.4 & java 8 & eclipse Luna SR1 (4.4.1) & oracle 11g.我正在使用 hibernate 5.0.4 & java 8 & eclipse Luna SR1 (4.4.1) & oracle 11g。

MainClass is:主类是:

public class MainMethod {
public static void main(String[] args) {
    SampleClass s = new SampleClass();
    s.setId(1);
    s.setValue("Value_1");

    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

    Session session = sessionFactory.openSession();
    session.beginTransaction();
    session.save(s);
    session.getTransaction().commit();      
}
}

the hibernate.cfg.xml is hibernate.cfg.xml 是

<?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">oracle.jdbc.driver.OracleDriver</property>
  <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:mndb11g</property>
  <property name="hibernate.connection.username">temp_p</property>
  <property name="hibernate.connection.password">temp_p</property>
  <property name="connection.pool_size">1</property>

  <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
  <property name="hibernate.default_schema">temp_p</property>

  <property name="hbm2ddl.auto">create</property>
  <property name="show_sql">true</property>

  <mapping class="com.h.SampleClass.SampleClass"/>

</session-factory>
</hibernate-configuration>

Its giving an exception at它给出了一个例外

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

The exception stack trace is异常堆栈跟踪是

    Exception in thread "main" java.lang.NullPointerException
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.getResources(ClassLoaderServiceImpl.java:173)
    at java.util.ServiceLoader$LazyIterator.hasNextService(ServiceLoader.java:348)
    at java.util.ServiceLoader$LazyIterator.hasNext(ServiceLoader.java:393)
    at java.util.ServiceLoader$1.hasNext(ServiceLoader.java:474)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.loadJavaServices(ClassLoaderServiceImpl.java:324)
    at org.hibernate.integrator.internal.IntegratorServiceImpl.<init>(IntegratorServiceImpl.java:40)
    at org.hibernate.boot.registry.BootstrapServiceRegistryBuilder.build(BootstrapServiceRegistryBuilder.java:213)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:119)
    at com.h.MainMethod.main(MainMethod.java:19)

Can anyone please help to solve this.任何人都可以请帮助解决这个问题。 Thanks in advance.提前致谢。

This could happen due to several reasons.这可能由于多种原因而发生。

  • May be the Entity cannot be found.可能是找不到实体。 In your hibernate.cfg.xml file the entity is mentioned as com.h.SampleClass.SampleClass Please double check the class name and the package name.在您的hibernate.cfg.xml文件中,实体被称为com.h.SampleClass.SampleClass请仔细检查类名和包名。

  • Make sure that the hibernate libraries are added as user libraries.确保将休眠库添加为用户库。 Not system libraries.不是系统库。 More info 更多信息

Hope this helps.希望这可以帮助。

It sounds like hibernate.cfg.xml is not on your classpath when the code runs.当代码运行时,听起来 hibernate.cfg.xml 不在您的类路径中。 If you are using maven put it in src/main/resources.如果您使用的是 maven,请将其放在 src/main/resources 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM