我刚刚开始学习Hibernate。为此,我编写了一个简单的Java程序。 但是当我尝试执行程序时,我会遇到异常。
Apr 30, 2014 1:54:18 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Apr 30, 2014 1:54:18 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.5.Final}
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.hibernate.cfg.Configuration.reset(Configuration.java:324)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:289)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:293)
at com.hiber.main.Main.main(Main.java:19)
Caused by: java.lang.NullPointerException
at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
at org.hibernate.cfg.Environment.<clinit>(Environment.java:221)
... 4 more
以下是该计划:
package com.hiber.main;
import javax.naming.NamingException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class Main {
public static void main(String[] args) throws NamingException {
//SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(new StandardServiceRegistryBuilder().build());
ServiceRegistry serviceRegistry;
SessionFactory sessionFactory;
Configuration configuration = new Configuration();
configuration.configure("/com/hiber/main/hibernate.cfg.xml");
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session sess = sessionFactory.getCurrentSession();
sessionFactory.openSession();
sess.beginTransaction();
Test1 t = new Test1();
t.setTest1Ind(1);
t.setName("name");
t.setPassword("abcd1234");
sess.saveOrUpdate(t);
sess.getTransaction().commit();
sess.close();
}
}
以下是cfg xml:
<!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.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root1234</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">test</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
我已经包含了所有依赖项。希望我没有错过任何东西。 目前无法添加图片。
请任何人看看代码,并考虑一下这里可能出现的问题。 提前谢谢。
答案 0 :(得分:0)
在类路径中使用hibernate.cfg.xml并尝试使用这样的代码
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
例如,如果您使用maven,它将位于\src\main\resources\hibernate.cfg.xml
<强>更新强>
检查您是否具有正确的依赖关系(jar)。 我在我的maven项目中有这些依赖项
答案 1 :(得分:0)
完成本教程here
使用Annotation逐步进行Hibernate Mapping。
答案 2 :(得分:0)
我想你忘记了hibernate cfg的映射类。
在标记
之前添加映射类</session-factory>
在您的hibernate cfg上,不要忘记在课堂上添加注释。
,映射如下所示:
<mapping class="Curriculums"/>
<mapping class="CurriculumsHasStudentId"/>
<mapping class="Students"/>
带有注释的java类如下所示:
@Entity
@Table(name = "students")
public class Students implements Serializable{
@Id
@Column(name="studentsNrp", unique=true)
private String studentsNrp;
@Column(name = "studentsName")
private String studentsName;
@Column(name = "studentsAddress")
private String studentsAddress;
@Column(name = "studentsCity")
private String studentsCity;
@Column(name = "studentsPhone")
private String studentsPhone;
答案 3 :(得分:0)
1删除项目中的依赖项。 2添加正确的依赖项(jar)。