运行时的Hibernate配置

Ole*_*ndr 16 java hibernate

我有hibernate.cfg.xml文件.

<session-factory>

    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url"></property>
    <property name="connection.username"></property>
    <property name="connection.password"></property> 

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>
Run Code Online (Sandbox Code Playgroud)

.....................

这是文件中最有趣的部分.现在我必须设置缺失值:url,username,password.我试图这样做:

public static void SetSessionFactory() {
    try {

      AnnotationConfiguration conf = new AnnotationConfiguration().configure();
      // <!-- Database connection settings -->
      conf.setProperty("connection.url", URL);
      conf.setProperty("connection.username", USERNAME);
      conf.setProperty("connection.password", PASSWORD);
      SESSION_FACTORY = conf.buildSessionFactory();

    } catch (Throwable ex) {
      // Log exception!
      throw new ExceptionInInitializerError(ex);
    }
  }
Run Code Online (Sandbox Code Playgroud)

但它只是从hibernate.cfg.xm加载我的配置,并且不更改任何属性...

url,username,passoword - 是命令行参数,所以我必须在运行时设置它们.

Ale*_*kov 23

试着打电话conf.configure();到这里.
并且属性可能需要具有hibernate前缀,例如"hibernate.connection.username"
希望它有所帮助.