Я новичок в спящем режиме и пытаюсь что-то кодировать.Я застреваю с ошибкой (Не удалось создать sessionFactory object.java.lang.NullPointerException)
Некоторая помощь будет высоко оценена.
hibernate.cfg.xml Файл:
<?xml version='1.0' encoding='utf-8'?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!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>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521/orclpdb1</property>
<property name="connection.username">oracle</property>
<property name="connection.password">admin</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping class="ProjectHib.dto.Client"/>
</session-factory>
</hibernate-configuration>
Файл Client.hbm.xml:
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name = "Client" table = "Client">
<meta attribute = "class-description">
This class contains the employee detail.
</meta>
<id name = "ClientID" type = "int" column = "id">
<generator class="native"/>
</id>
<property name = "name" column = "name" type = "string"/>
</hibernate-mapping>
А затем файл Client.java:
package ProjectHib.dto;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Client {
@Id
private int ClientID;
private String name;
public Client() {}
public Client(int clientID, String name) {
ClientID = clientID;
this.name = name;
}
public int getClientID() {
return ClientID;
}
public void setClientID(int clientID) {
ClientID = clientID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Наконец, тестовый файл Hibernate:
package ProjectHib.dto;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class TestHibernate {
private static SessionFactory factory;
public static void main(String[] args) {
Client C1 = new Client();
C1.setClientID(0);
C1.setName("fat7i 1");
try {
factory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
Session session = factory.openSession();
session.beginTransaction();
session.save(C1);
session.getTransaction().commit();
}
}
Редактировать: я следовал учебному пособию .. Цель этого кода - сохранить (сохранить) объект (клиент C1) в базе данных Oracle (я не знаю, еслиэто немного проясняет)