DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Custom Model Context Protocol (MCP) for NL2SQL: A Rigorous Evaluation Framework on Oracle Database
  • Applying Oracle 19c Release Update (RU): A Practical Guide from My DBA Experience
  • Chat with Your Oracle Database: SQLcl MCP + GitHub Copilot
  • Understanding Bigfile Tablespace Defaults in Oracle Database 23ai: Impact and Benefits

Trending

  • Introduction to Retrieval Augmented Generation (RAG)
  • Leveraging Apache Flink Dashboard for Real-Time Data Processing in AWS Apache Flink Managed Service
  • From APIs to Actions: Rethinking Back-End Design for Agents
  • Tactical Domain-Driven Design: Bringing Strategy to Code
  1. DZone
  2. Data Engineering
  3. Databases
  4. Oracle Database Link and Hibernate Mapping

Oracle Database Link and Hibernate Mapping

The article is to describe how to create a Database link in Oracle and use Hibernate to create mapping for the remote table.

By 
Eason YIN user avatar
Eason YIN
·
Oct. 06, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
7.1K Views

Join the DZone community and get the full member experience.

Join For Free

The article is to describe how to create a Database link in Oracle and use Hibernate to create mapping for the remote table.

Grant Privilege

Grant create/drop database link privilege to a user, and we also need the grant to create view privilege for the user

Run below SQL by SYSDBA

SQL> GRANT CREATE PUBLIC DATABASE LINK TO <USER>;

SQL> GRANT DROP PUBLIC DATABASE LINK TO <USER>;

SQL> GRANT CREATE VIEW TO <USER>;

Database Link

Login and run below SQL to create a database link 

Java
 




x


 
1
CREATE <PUBLIC> DATABASE LINK <DB_LINK> CONNECT TO <DATABASE_USER> IDENTIFIED BY <DATABASE_PASSWORD> USING '(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = <DATABASE HOSTNAME>)(PORT = <PORT>)))(CONNECT_DATA = (SERVICE_NAME = <SERVICE NAME>)))';



Attached Oracle document:   https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_5005.htm

Description of create_database_link.gif follows

Viewing Information About Database Links

Java
 




xxxxxxxxxx
1


 
1
SELECT * FROM DBA_DB_LINKS  #Lists all database links in the database.
2

          
3
SELECT * FROM ALL_DB_LINKS #Lists all database links accessible to the connected user.
4

          
5
SELECT * FROM USER_DB_LINKS  #Lists all database links owned by the connected user.



Delete Database Link

DROP <PUBLIC> DATABASE LINK <DB_LINK>;

View

Create View to map remote database table

CREATE VIEW <VIEW> AS (SELECT * FROM <REMOTE TABLE>@<DB_LINK>;

Hibernate Mapping

We can use a common way to create hibernate mapping to a Database View. For example

<hibernate-mapping>
<class name="com.xx.xxx.xxxx.hibernate.entity.view.XXXX"
table="<VIEW>" dynamic-update="true">
...
</class>
</hibernate-mapping>

SYNONYM

We also can create a synonym table instead of view after the Database link created

CREATE PUBLIC SYNONYM <SYNONYM>  FOR  <REMOTE TABLE>@<DB_LINK>;

Hibernate Mapping

We can map a synonym table like below

Java
 




xxxxxxxxxx
1


 
1
<hibernate-mapping>
2
    <class name="com.xx.xxx.xxx.hibernate.entity.view.XXXX"
3
           table="<Remote Table>@<DB_LINK>" dynamic-update="true">
4
           ...
5
    </class>
6
</hibernate-mapping>



Furthermore,  hibernate configuration need below changes

<!-- Add below one -->
<prop key="hibernate.synonyms">true</prop>

And hibernate need disable auto validation.

<!-- Remove below one -->
<!--prop key="hibernate.hbm2ddl.auto">validate</prop-->

Database Links Hibernate Oracle Database

Opinions expressed by DZone contributors are their own.

Related

  • Custom Model Context Protocol (MCP) for NL2SQL: A Rigorous Evaluation Framework on Oracle Database
  • Applying Oracle 19c Release Update (RU): A Practical Guide from My DBA Experience
  • Chat with Your Oracle Database: SQLcl MCP + GitHub Copilot
  • Understanding Bigfile Tablespace Defaults in Oracle Database 23ai: Impact and Benefits

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook