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

  • Claude Code Superpowers: The 5-Tool Stack That Ends Limits and Context Amnesia
  • How to Keep AI Agents From Going Rogue Using Docker Sandbox
  • Fix the Target, Precompute Once: A Backend-Free Word-Ladder Solver With a BFS Distance Field
  • Android Application Security: Android Application Architecture

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Oct. 06, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
7.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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