getting hibernate default schema name programmatically from session factory?

后端 未结 4 948
无人及你
无人及你 2021-02-07 15:24

I was wondering if there is a way to get the default schema name from session factory, somehow? The reason I need to get it is because I have to use a one native SQL and I have

4条回答
  •  孤城傲影
    2021-02-07 16:12

    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
      }
    

提交回复
热议问题