pikaraider
pikaraider

Reputation: 197

How to connect to Oracle 12c PDB using Hibernate

I have created a sample Oracle 12c PDB (Pluggable Data Base) using the instructions from here. How do I connect to this pluggable database using Hibernate application? I am using a sample Hibernate application from here

I changed the hibernate.cfg.xml file as follows:

<?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>

But I am getting the following error trace when I run the program:

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

Kindly let me know where I am going wrong. There are almost no resources online for using Oracle 12c PDB with Hibernate.

UPDATE 1: I had an extra line of XML code in my config file which lead to the XML parsing error. Now - how do I associate a CDB/PDB with a particular user account, since using the PDB through SYS user is not recommended.

I have a PDB named 'pdb1', and it is associated with sys user account. It is stored at the following location:

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

I created a new user 'c##test' and then created a pdb while logged in to the user 'c##test' using the following command:

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' is created successfully, but it is not getting associated to the user 'c##test'.

The error trace I am getting now is as follows: https://pastebin.com/skVMLkqT

Upvotes: 2

Views: 3561

Answers (1)

Mohammadreza  Alagheband
Mohammadreza Alagheband

Reputation: 2230

The issue is with the syntax you are using. you are using :SID instead of /SERVICE_NAME , so make sure to change this line as :

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

to see which services available, execute 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.


according to your stack trace you missed oracle driver dependency:

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

you should add this dependecy to your pom.xml file,Please take these steps for that:

1- Download ojdbc8.jar from oracle site:

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

2- Install ojdbc8 to your local maven repository:

  • specify you path instead of 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-Add Dependency to Pom.xml

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

Upvotes: 2

Related Questions