无法在Hibernate中创建sessionFactory(如何使用hibernate将对象保存到Oracle数据库中)

时间:2019-02-26 20:20:27

标签: java oracle hibernate

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

    }
}

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

1 个答案:

答案 0 :(得分:0)

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