gpt4 book ai didi

java - 如何使用 Hibernate 连接到 Oracle 12c PDB

转载 作者:行者123 更新时间:2023-11-30 06:01:03 24 4
gpt4 key购买 nike

我使用 here 中的说明创建了一个示例 Oracle 12c PDB(可插拔数据库) 。如何使用 Hibernate 应用程序连接到这个可插拔数据库?我正在使用 here 中的示例 Hibernate 应用程序

我更改了 hibernate.cfg.xml 文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>

<property name="connection.url">jdbc:oracle:thin:@localhost:1521:sys</property>
<property name="connection.username">sys as sysdba</property>
<property name="connection.password">helloWORLD12</property>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="dialect">org.hibernate.dialect.Oracle12cDialect</property>

<property name="show_sql">true</property>

<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<property name="current_session_context_class">thread</property>

<mapping class="net.codejava.hibernate.Book" />

</session-factory>
</hibernate-configuration>

但是当我运行程序时,我收到以下错误跟踪:

Exception in thread "main" org.hibernate.HibernateException: Error accessing stax stream
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:107)
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65)
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
at net.codejava.hibernate.BookManager.setup(BookManager.java:23)
at net.codejava.hibernate.BookManager.main(BookManager.java:100)
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[18,6]
Message: The processing instruction target matching "[xX][mM][lL]" is not allowed.
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:604)
at com.sun.xml.internal.stream.XMLEventReaderImpl.peek(XMLEventReaderImpl.java:276)
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:103)
... 5 more

请让我知道我哪里出错了。网上几乎没有关于将 Oracle 12c PDB 与 Hibernate 结合使用的资源。

更新 1:我的配置文件中有一行额外的 XML 代码,这会导致 XML 解析错误。现在 - 如何将 CDB/PDB 与特定用户帐户关联,因为不建议通过 SYS 用户使用 PDB。

我有一个名为“pdb1”的 PDB,它与 sys 用户帐户关联。它存储在以下位置:

D:\app\myusername\virtual\oradata\orcl\pdb1

我创建了一个新用户“c##test”,然后使用以下命令在登录用户“c##test”时创建了一个 pdb:

create pluggable database pdb3 admin user pdb_admin3 identified by helloWORLD12 
file_name_convert=('D:\app\myusername\virtual\oradata\orcl\pdbseed\',
'D:\app\myusername\virtual\oradata\test\pdb3\');

“pdb3”已成功创建,但未与用户“c##test”关联。

我现在得到的错误跟踪如下:https://pastebin.com/skVMLkqT

最佳答案

问题出在您使用的语法上。您正在使用 :SID 而不是/SERVICE_NAME ,因此请确保将此行更改为:

<property name="connection.url">jdbc:oracle:thin:@localhost:1521/sys</property>

要查看哪些服务可用,请执行lsnrctl service

Please also note that you are trying to use sys account which is a the same as root. to avoid future problems, it's best practice to create a new account an use that instead.

<小时/>

根据您的堆栈跟踪,您错过了 oracle 驱动程序依赖项:

ClassNotFoundException: Could not load requested class : oracle.jdbc.OracleDriver

您应该将此依赖项添加到您的 pom.xml 文件中,请执行以下步骤:

1-从oracle站点下载ojdbc8.jar:

https://www.oracle.com/technetwork/database/features/jdbc/jdbc-ucp-122-3110062.html

2- 将 ojdbc8 安装到本地 Maven 存储库:

  • 指定您的路径,而不是Path/to/your/

    mvn install:install-file -Dfile={Path/to/your/ojdbc8.jar} DgroupId=com.oracle -DartifactId=ojdbc8 -Dversion=12.2.0.1 -Dpackaging=jar

3-向Pom.xml添加依赖

<!-- ORACLE database driver -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>
</dependency>

关于java - 如何使用 Hibernate 连接到 Oracle 12c PDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52269157/

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