首页 > 解决方案 > 在 Hibernate 中创建 sessionFactory 失败(如何使用 hibernate 将对象保存到 Oracle 数据库中)

问题描述

我是休眠新手,正在尝试编写一些代码。我遇到了一个错误(无法创建 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();

    }
}

编辑:我正在关注一个教程..这段代码的目的是在 Oracle 数据库中保存(持久)对象(Clien C1)(我不知道这是否能澄清一些事情)

标签: javaoraclehibernate

解决方案


尝试插入“oracle.jdbc.driver.OracleDriver”作为“connection.driver_class”而不是“oracle.jdbc.driver”。这仅适用于 JDK 兼容的 Java 版本。


推荐阅读