6

这是我在 hibernate.cfg.xml 中的内容

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</property>
        <property name="hibernate.connection.charSet">UTF-8</property> 
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="connection.pool_size">1</property>
        <property name="show_sql">true</property>
        <property name="hibernate.use_outer_join">true</property>
        <property name="current_session_context_class">thread</property>
    </session-factory>
</hibernate-configuration>

另外,我正在动态覆盖一些属性......

 Configuration config = new Configuration().configure("path_to_hibernate.cfg.xml");
 config.setProperty("hibernate.connection.url", System.getenv("HEROKU_POSTGRESQL_MYCOLOR_URL"));
 config.setProperty("hibernate.connection.username", "***");
 config.setProperty("hibernate.connection.password", "***");

但是,当我运行它时,我得到了这个错误......

ERROR: No suitable driver found for postgres://*******:*********@ec2-23-21-85-197.compute-1.amazonaws.com:5432/d9i5vp******o7te

如何配置我的属性以便 heroku 找到 postgres 驱动程序?

(我是hibernate和heroku的新手,所以非常感谢任何帮助:)

4

3 回答 3

2

您可以将此 Hibernate+JPApersistence.xml配置用作您的 hibernate 配置的基础。

hibernate.cfg.xmlthe和 the之间的属性名称persistence.xml是相同的,只有 hibernate 使用打开和关闭元素,而 JPA 使用属性。

<properties>
            <property name="hibernate.connection.url" value="jdbc:postgresql://ec2-107-21-126-162.compute-1.amazonaws.com:6232/dbname?username=username&amp;password=password&amp;ssl=true&amp;sslfactory=org.postgresql.ssl.NonValidatingFactory"/>
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
            <property name="hibernate.connection.username" value="username"/>
            <property name="hibernate.connection.password" value="password"/>
            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hbm2ddl.auto" value="update"/>

            <!-- c3p0 connection pool settings -->
            <property name="hibernate.connection.provider_class" value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" />
            <property name="hibernate.c3p0.min_size" value="1" />
            <property name="hibernate.c3p0.max_size" value="5" />
            <property name="hibernate.c3p0.acquire_increment" value="1" />
            <property name="hibernate.c3p0.idle_test_period" value="3000" />
            <property name="hibernate.c3p0.max_statements" value="50" />
            <property name="hibernate.c3p0.timeout" value="1800" />

        </properties>

Maven依赖:

 <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.2.1.Final</version>
        </dependency>       

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>4.2.1.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.2.1.Final</version>
        </dependency>
于 2013-05-17T15:27:42.090 回答
0

来自 Heroku Postgres 的 URL 格式不是 JDBC 格式。它是一种多语言格式,因此所有平台都可以使用它。因此需要将 URL 转换为 JDBC 格式。在 Heroku 开发中心有一个很好的例子:
https ://devcenter.heroku.com/articles/connecting-to-relational-databases-on-heroku-with-java#using-the-in-纯 jdbc

于 2012-08-21T12:44:37.817 回答
0
        Map<String, String> jdbcUrlSettings = new HashMap<>();
        String jdbcDbUrl = System.getenv("JDBC_DATABASE_URL");
        System.out.println("jdbcDbUrl" + jdbcDbUrl);
        if (null != jdbcDbUrl) {
            jdbcUrlSettings.put("hibernate.connection.url", System.getenv("JDBC_DATABASE_URL"));
        }



        Configuration configuration = new Configuration();
        annotatedAddClass(configuration);

        StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
        serviceRegistryBuilder.applySettings(jdbcUrlSettings);
        ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
        return configuration.buildSessionFactory(serviceRegistry);
于 2020-04-10T10:22:56.537 回答