gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:50:04 24 4
gpt4 key购买 nike

我是 hibernate 新手,正在尝试编写一些代码。我遇到了错误( Failed to create 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数据库中(我不知道这是否澄清了一些事情)

最佳答案

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

关于java - 无法在 Hibernate 中创建 sessionFactory(如何使用 hibernate 将对象保存到 Oracle 数据库中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54893474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com