[ACCEPTED]-getting hibernate default schema name programmatically from session factory?-sessionfactory
I just found out that hibernate has {h-schema} replacement 8 that can be used in native sql queries. So 7 this does the job cleanly when you are connected 6 to a one schema in oracle database and want 5 to execute queries against different schemas. Example 4 would be:
select * from {h-schema}table_name
This ways instead of doing a manual 3 replaceAll
in a query, hibernate will take care of 2 everything given that each session factory 1 is configured with "hibernate.default_schema"
property.
I had problems with John's solution to use 5 {h-schema} when using the Criteria api's 4 Restrictions.sqlRestriction(...) (probably 3 because this substitution happens within 2 the separate HQL api). Similar to Michael's 1 solution, I used:
SessionFactoryImplementor sfi = (SessionFactoryImplementor)sessionFactory;
String name = sfi.getSettings().getDefaultSchemaName();
This will do the trick:
SessionFactoryImplementor sfi = (SessionFactoryImplementor) getSessionFactory();
Settings settings = sfi.getSettings();
ConnectionProvider connectionProvider = settings.getConnectionProvider();
try {
Connection connection = connectionProvider.getConnection();
DatabaseMetaData databaseMetaData = connection.getMetaData();
String url = databaseMetaData.getURL();
//substring the string to what you want
System.out.println(url);
} catch (SQLException e) {
//throw something
}
0
@Ryan Morlok, I can not comment on your 3 answer due to lack of reputation. Your answer 2 is correct, but its deprecated now. In hibernate 1 version 5.0.1, schema name can be get using
(SessionFactoryImpl)sessionFactory).getProperties().getProperty("hibernate.default_schema")
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.