Dave Ranjan
Dave Ranjan

Reputation: 2984

How to select schema at runtime using Hibernate?

I already have an existing code base, where schema(like db_1, db_2..) are created at run time.

We are currently using JdbcTemplate, using that its quite easy to append schema in the native SQL queries some thing like :-

sql = " Select * from "+schema+".user";
jdbcTemplate.query(sql, new UserMapper());

Now I want to know is how to provide schema to hibernate at runtime like I did with the jdbcTemplate?

What connection url should I provide in hibernate.cfg.xml so that it doesn't connects to a single schema rather whole database?

Any suggestions will be helpfull.

P.S: I am new to hibernate (So I might have missed something stupid)

Upvotes: 2

Views: 6882

Answers (1)

Yaroslav Stavnichiy
Yaroslav Stavnichiy

Reputation: 21456

I know of two options:

  1. Use native SQL query binding results to JPA entities. Details here.

  2. Use Hibernate multi-tenancy. Details here and here.

Although I haven't tried either.

Upvotes: 9

Related Questions