[英]Hibernate 5 “main” java.lang.NullPointerException
I create a small java class for hibernate but its gives an exception.I'm using Hibernate 5.0.2 and java 8 on ubuntu 14.04. 我为休眠创建了一个小的Java类,但它给出了异常。我在ubuntu 14.04上使用了Hibernate 5.0.2和Java 8。 My classes and Hibernate configuration files is as follows.Hope someone can help me to find a solution to this.
我的类和Hibernate配置文件如下。希望有人可以帮助我找到解决方案。
This is the exception that occurs. 这是发生的异常。
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:212)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:119)
at com.gim.gimpro.HibernateTest.setUp(HibernateTest.java:43)
at com.gim.gimpro.HibernateTest.main(HibernateTest.java:18)
Here is my code 这是我的代码
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.gim.hibe.UserDetails;
public class HibernateTest {
private SessionFactory sessionFactory;
public static void main(String[] args){
HibernateTest hibernateTest=new HibernateTest();
try {
hibernateTest.setUp();
hibernateTest.save();
} catch (Exception e) {
e.printStackTrace();
}
}
void save(){
UserDetails userDetails=new UserDetails();
userDetails.setUserId(1);
userDetails.setUserName("Gihan");
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save( userDetails );
session.getTransaction().commit();
session.close();
}
protected void setUp() throws Exception {
sessionFactory = new Configuration().configure() .buildSessionFactory();
}
}
UserDetails class UserDetails类
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "USER_DETAILS")
public class UserDetails {
@Id
private int userId;
private String userName;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
Hibernate Configuration 休眠配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 5.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedb</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.hbm2ddl.auto">create</property> <mapping class="com.gim.hibe.UserDetails"/> </session-factory> </hibernate-configuration>
This Exception can occur because of below 2 reasons: 可能由于以下两个原因而发生此异常:
Please check if package com.gim.hibe for UserDetails class is correct. 请检查UserDetails类的com.gim.hibe软件包是否正确。
Also see if Hibernate libraries are properly included in the classpath. 另请参阅Hibernate库是否正确包含在类路径中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.