Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 612
Dave Ranjan Avatar asked Jan 28 '26 04:01

Dave Ranjan


1 Answers

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.

like image 164
Yaroslav Stavnichiy Avatar answered Jan 29 '26 16:01

Yaroslav Stavnichiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!