0

Based on my previous unsolved question PostgreSql does not "recognize" table created by Hibernate I would like to ask if I do not do something right regarding the connection to PostgreSql database from my hibernate application. I am inclined to believe that the problem lies in the connection to the database and I could not solve it in the last two days. The Entities are as shown in my linked question (I do not write the again due to the space demand). My cfg.xml file is:

    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.url">jdbc:postgresql:template1</property>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="hibernate.connection.username">postgres</property>
    <property name="hibernate.connection.password">user</property>


    <property name="connection.pool_size">1</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.hbm2dll.auto">create</property>
    <property name="current_session_context_class">thread</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

    <mapping class="com.al.hibernatetest.Department"/>
    <mapping class="com.al.hibernatetest.Employee"/>
</session-factory>

I have seen the url connection from the hibernate distribution configuration file, but I have also tried it with jdbc:postgresql:testDB as well. (testDB: name of my database).

The error which comes out in Eclipse is:

Apr 27, 2013 11:54:58 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Apr 27, 2013 11:54:58 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.0.Beta1}
Apr 27, 2013 11:54:58 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Apr 27, 2013 11:54:58 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Apr 27, 2013 11:54:58 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Apr 27, 2013 11:54:58 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Apr 27, 2013 11:54:58 AM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Apr 27, 2013 11:54:59 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Apr 27, 2013 11:54:59 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 1
Apr 27, 2013 11:54:59 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
Apr 27, 2013 11:54:59 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [org.postgresql.Driver] at URL [jdbc:postgresql:template1]
Apr 27, 2013 11:54:59 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=postgres, password=****}
Apr 27, 2013 11:54:59 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
Apr 27, 2013 11:54:59 AM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
Apr 27, 2013 11:54:59 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Apr 27, 2013 11:54:59 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: insert into DEPARTMENT (DEPT_NAME) values (?)
Apr 27, 2013 11:54:59 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 42P01
Apr 27, 2013 11:54:59 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ERROR: relation "department" does not exist
  Position: 13
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:136)
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2967)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3478)
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:203)
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:183)
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:167)
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:321)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:286)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:192)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:205)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:190)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:91)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:764)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:756)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:752)
    at com.al.hibernatetest.Main.main(Main.java:24)
Caused by: org.postgresql.util.PSQLException: ERROR: relation "department" does not exist
  Position: 13
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1886)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:555)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:363)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:133)
    ... 22 more

The PostgreSql log file contains always the message:

2013-04-27 11:36:51 CEST ERROR:  relation "department" does not exist at character 13
2013-04-27 11:36:51 CEST STATEMENT:  insert into DEPARTMENT (DEPT_NAME) values ($1) RETURNING *
2013-04-27 11:36:51 CEST LOG:  could not receive data from client: No connection could be made because the target machine actively refused it.

Any idea what is the cause of the problem? Waht is wrong with the connection to database? Can it be a missing jar, or in other words not using the suitable jars?

P.S.: Some people assumed thet the problem is the Upper/Lower case of the tables names.I have tried though to change all the mapped names from Upper to Lower and vice versa in the entities, but it did not change anything.

4

2 回答 2

2

根据我在您的其他问题中的建议:

启动 psql your_database 并发出 \d 以查看表。

我对 Hibernate 不熟悉,但很可能诸如 @Table(name="DEPARTMENT") 之类的语句正在创建实际上称为“DEPARTMENT”的表,而您的查询实际上是从 DEPARTMENT 查询(不带双引号),这postgresql 将解释为部门。

如果是这样,解决方法是在创建表名时使用小写字母,或者让 Hibernate 在查询表时使用双引号和大写字母。

于 2013-04-27T10:27:48.340 回答
1

我有类似的问题,我的表名是 Message_History 并且我收到的错误消息是“由:org.postgresql.util.PSQLException:错误:关系“message_history”不存在”,我不知道为什么它看起来是 message_history我的休眠配置将表名称为 Message_History。因为这是我的示例代码,所以我将 DB 中的表名更改为 message_history,然后继续,但是如果列名中的下/上护理也有类似的问题。所以我将列名更改为小写,然后它对我来说非常好。

于 2014-06-04T17:15:14.633 回答