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.

8
  • Your connection does work, but the table department is not available. You need to create the table first using a create table statement Commented Apr 27, 2013 at 10:05
  • 1
    @a_horse_with_no_name: That is the work of Hibernate. To this effect we add the property <property name="hibernate.hbm2dll.auto">create</property> in the cfg.xml file as I cite in my question. Commented Apr 27, 2013 at 10:14
  • So apparently Hibernate is not creating your table. I have asked this already in the other question: is the table visible in psql? Commented Apr 27, 2013 at 10:16
  • No it is not(in pdAdmin I can see it). Therefore I am inclined to believe that the application has no "access" to the database. Commented Apr 27, 2013 at 10:23
  • The table is not visible in psql, but in pgAdmin? That doesn't make sense. And it's clearly not a "connection" problem. You'd get a totally different error message there. My guess is that you are connecting to a different database/server in pgAdmin and your application. Commented Apr 27, 2013 at 10:24

2 Answers 2

2

Per my suggestion in your other question:

Start psql your_database and issue \d to see the tables.

I'm unfamiliar with Hibernate, but chances are that statements such as @Table(name="DEPARTMENT") are creating tables that are actually called "DEPARTMENT", while your queries are actually querying from DEPARTMENT (without the double quotes), which postgresql will interpret as department.

If so, the fix is to either use lowercase for your table names when creating them, or to make Hibernate use double-quotes and caps when querying your tables.

Sign up to request clarification or add additional context in comments.

7 Comments

The table does not exist because Hibernate has not created it, as it is supposed to do. Regarding the lower/upper case, it is not the cause. Even if I remove the annotations @Table(name="...") and @Column(name="...") I get the same error: "Relation department does not exist". Due to an unknown reason, hibernate does not creates the Table. Someone experienced in Hibernate can probably explain and I am surprised that people of the site spesializing in Hibernate have not responded...
Double-check (directly in the DB, using psql) that the table is not being created with a capitalized name. Because if it is, you'd get the message that the table does not exist when, in fact, it merely exists but with a different name. Also, check which schemas exist, in case your search path is getting set with some weird value.
I have checked if the table exists many times. Even with the query Select * from DEPARTMENT in pgAdmin I get the same Error: "relation "department" does not exist". Out of an unknown reason it does not create the tables. In Schema I have Schema(1)-public
That's not checking... :-) Select * from DEPARTMENT translates to SELECT * FROM departments. What you'd need to check is for SELECT * FROM "Department" or SELECT * FROM "DEPARTMENT". The double quotes makes things case sensitive and tells PG to not normalize relnames.
Also, take a peek into the information schema: postgresql.org/docs/9.2/static/infoschema-tables.html. For instance select * from information_schema.tables where table_name like 'd%' or table_name like 'D%'.
|
1

I had similar issue, where my table name was Message_History and error message which i was getting is "Caused by: org.postgresql.util.PSQLException: ERROR: relation "message_history" does not exist", i am not sure why its lookig message_history where my hibernate configuration says table name as Message_History. since this was my sample code so i changed the table name in DB as message_history and then continued , but again i got similar issue if lower/upper care in column names too. so i changed columns names to lower case and then it worked perfectly fine to me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.