hibernate.cfg.xml的db属性

时间:2017-04-25 16:38:09

标签: java spring postgresql hibernate

我尝试使用Java 1.8中的hibernate连接到postgresql数据库。

我的hibernate.cfg.xml包含我在db.properties文件中定义的变量。

我尝试了几种方法,但它对我不起作用。

我的代码:

    File f = new File("hibernate.cfg.xml");
    java.util.Properties properties = new Properties();
    try {
        properties.load(new FileInputStream("db.properties"));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println(properties.getProperty("db.addr"));
    System.out.println(properties.getProperty("db.port"));

    Configuration configuration = new Configuration(); configuration.configure(f).addProperties(properties);

    ServiceRegistryBuilder registry = new ServiceRegistryBuilder();
    registry.applySettings(configuration.getProperties());
    ServiceRegistry serviceRegistry = registry.buildServiceRegistry();

    // loads configuration and mappings
    SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);


       // obtains the session
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    .....


    session.getTransaction().commit();
    session.close();

我的hibernate.cfg.xml如下:

    

    <!-- JDBC Properties -->

    <property name="connection.driver_class">
        org.postgresql.Driver
    </property>
    <property name="dialect">
        org.hibernate.spatial.dialect.postgis.PostgisDialect
    </property>
    <property name="connection.url">
        jdbc:postgresql://${db.addr}:${db.port}/metadata
    </property>
     ..........

........

配置仍然使用$ {db.addr}:$ {db.port}

[main] DEBUG org.hibernate.cfg.Configuration - connection.url = jdbc:postgresql:// $ {db.addr}:$ {db.port} / metadata

我错过了什么吗?

非常感谢

0 个答案:

没有答案