Hibernate连接到Redshift jdbc

时间:2016-11-11 10:34:02

标签: hibernate amazon-redshift

我正在使用Hibernate来测试一个简单的测试,以便连接到Amazon Redshift并在表中创建插入。 hibernate.cfg.xml文件如下所示

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->

        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://redshift_host:5439/dbname</property>
        <property name="connection.username">username</property>
        <property name="connection.password">my_password</property>

      

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

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.PostgreSQL82Dialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

        <mapping resource="com/ranga/mapping/Person.hbm.xml" />
    </session-factory>
</hibernate-configuration>

我得到以下错误

Nov 11, 2016 12:26:40 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
Nov 11, 2016 12:26:40 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: HHH000319: Could not get database metadata
org.postgresql.util.PSQLException: The connection attempt failed.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:225)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:136)

连接网址和凭据没有错。我测试了同样的网址和其他应用中的凭据。

此外我忘了提到Redshift版本是v8.0.2并且我使用以下依赖项:

 <dependency>
      <groupId>postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>8.0-318.jdbc3</version>
    </dependency>

你能帮帮我吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

我可以在persistence.xml中使用以下配置进行访问:

<persistence-unit name="accounts2" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:/AccountMovementsRS</jta-data-source>
     <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect" />
        <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
        <property name="hibernate.default_schema" value="rsds_ops_latam"/>
        <property name="hibernate.connection.url" value="myconnection" />
        <property name="hibernate.connection.username" value="user" />
        <property name="hibernate.connection.password" value="password" />
         <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.flushMode" value="FLUSH_AUTO" />
        <property name="hibernate.hbm2ddl.auto" value="validate" />
     </properties>
</persistence-unit>

让我知道它是否对您有帮助!