Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing inter-schema tables and relations in hibernate

There is a typical situation being faced where different tables are scattered through different schemas in Oracle database and they are related to each other (encompassing all different types of relations).

How can they be represented in Hibernate using annotations as when a sessionfactory handle is created for one schema, tables in that schema can't access other related tables (foreign key relation to tables in other schema)?

For a query like following, exception is thrown -

"from table1 as model where model.table2Name.table2column = "+foo

Exception comes as -

org.hibernate.QueryException: 
    could not resolve property: 
    table2column of: 
    com.test.table1 
    [from com.test.table1 as model where model.table2Name.table2column = 1]

Here table1 and table2 are present in different schemas.

like image 460
nitesh Avatar asked Jun 11 '10 16:06

nitesh


People also ask

How would you use hibernate for two different schemas in a single database?

You can specify it by schema element while defining table for your entity. Else, you can use separate EntityManager pointing to respective schema & then use the same entity, as their structure is similar.

What is O R mapping how it is implemented using hibernate explain with example?

If an entity or class has collection of values for a particular variable, then we can map those values using any one of the collection interfaces available in java. Hibernate can persist instances of java. util. Map, java.

What is persistence in hibernate?

Java classes whose objects or instances will be stored in database tables are called persistent classes in Hibernate. Hibernate works best if these classes follow some simple rules, also known as the Plain Old Java Object (POJO) programming model.


1 Answers

Finally I got the solution. It is done using the schema annotation for that table as follows - @Entity @Table(name = "table1", schema="schema1") Also the mapping of table2 class should be included in configuration file of table1 schema.

like image 155
nitesh Avatar answered Oct 02 '22 07:10

nitesh