Mr.H.
Mr.H.

Reputation: 1055

Fully qualify Oracle table name in Hibernate

My Setup: I am using Hibernate 5 with the Oracle JDBC driver oracle.jdbc.driver.OracleDriver and dialect org.hibernate.dialect.Oracle9iDialect.

My modelclasses look like this:

@Entity
@Table(name = "MyModel1", catalog = "DB1")
public class MyModel1 {...}

@Entity
@Table(name = "MyModel2", catalog = "DB2")
public class MyModel2 {...}

My Problem: I have multiple modelclasses that are spread about different DBs on the same DBserver. I have to use a single DB-connection to query all of my modelclasses so I connect to DB1 for querying. Everything would be fine, if Hibernate would generate SQL queries like

select * from DB1.MyModel1;

but it does not. For some reason it makes its queries without the catalog ie.

select * from MyModel1;

which is fine for MyModel1 because I connect to DB1 but I need a fully qualified query for MyModel2 otherwise it throws an exception, because the table of MyModel2 cannot be found in DB1.

Do you know any way to trick Hibernate or JPA or the dialect into building queries with fully qualified tablenames?

Upvotes: 0

Views: 880

Answers (1)

Simon Martinelli
Simon Martinelli

Reputation: 36163

With Oracle you have to use the schema attribute:

@Entity
@Table(name = "MyModel1", schema= "DB1")
public class MyModel1 {...}

@Entity
@Table(name = "MyModel2", schema= "DB2")
public class MyModel2 {...}

Upvotes: 2

Related Questions