[ACCEPTED]-getting hibernate default schema name programmatically from session factory?-sessionfactory

Accepted answer
Score: 27

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.

Score: 8

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();
Score: 1

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
  } catch (SQLException e) {
       //throw something


Score: 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


More Related questions