Sergey Mikhanov Sergey Mikhanov - 7 days ago 8
Java Question

Hibernate 3: unable to query PostgreSQL database

I am setting up a project using

Hibernate 3.3.1 GA
and
PostgreSQL 8.3
. I've just created a database, the first table, added one row there and now configuring Hibernate.

However, even the simplest query:

Criteria criteria = session.createCriteria(Place.class);
List result = criteria.list();


could not be executed (empty list is returned though there is one record in the database). I looked at the PostgreSQL logs to see:

2008-09-17 22:52:59 CEST LOG: connection received: host=192.168.175.1 port=2670
2008-09-17 22:52:59 CEST LOG: connection authorized: user=... database=...
2008-09-17 22:53:00 CEST LOG: execute <unnamed>: SHOW TRANSACTION ISOLATION LEVEL
2008-09-17 22:53:02 CEST LOG: could not receive data from client: Connection reset by peer
2008-09-17 22:53:02 CEST LOG: unexpected EOF on client connection
2008-09-17 22:53:02 CEST LOG: disconnection: session time: 0:00:03.011 user=... database=... host=192.168.175.1 port=2670


I wrote a simple program using plain JDBC to fetch the same data and it worked. PostgreSQL logs in this case look like this (for comparison):

2008-09-17 22:52:24 CEST LOG: connection received: host=192.168.175.1 port=2668
2008-09-17 22:52:24 CEST LOG: connection authorized: user=... database=...
2008-09-17 22:52:25 CEST LOG: execute <unnamed>: SELECT * from PLACE
2008-09-17 22:52:25 CEST LOG: disconnection: session time: 0:00:00.456 user=... database=... host=192.168.175.1 port=2668


Hibernate debug log does not indicate any errors. If I take the query listed in the logs:


15:17:01,859 DEBUG org.hibernate.loader.entity.EntityLoader: Static select for entity com.example.data.Place: select place0_.ID as ID0_0_, place0_.NAME as NAME0_0_, place0_.LATITUDE as LATITUDE0_0_, place0_.LONGITUDE as LONGITUDE0_0_ from PLACE place0_ where place0_.ID=?


and execute it agains the database in the psql, it works (this means that Hibernate has generated a proper SQL).

Below is the Hibernate configuration:

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:postgresql://192.168.175.128:5433/...</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.username">...</property>
<property name="hibernate.connection.password">...</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.use_outer_join">true</property>

<mapping resource="com/example/data/Place.hbm.xml"/>
</session-factory>
</hibernate-configuration>


...and the mapping file:

<hibernate-mapping package="com.example.data">
<class name="com.example.data.Place" table="PLACE">
<id column="ID" name="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property column="NAME" name="name" not-null="true" type="java.lang.String">
<meta attribute="use-in-tostring">true</meta>
</property>
<property column="LATITUDE" name="latitude" not-null="true" type="java.lang.Float">
<meta attribute="use-in-tostring">true</meta>
</property>
<property column="LONGITUDE" name="longitude" not-null="true" type="java.lang.Float">
<meta attribute="use-in-tostring">true</meta>
</property>
</class>
</hibernate-mapping>


Googling for
unexpected EOF
log entry was not friutful. Any ideas, community?

Answer

After applying debugger to the Hibernate code, it is fixed!

It is not visible in the question's text, but the problem is that Place passed to the createCriteria() method is from another package, not com/example/data, specified in the configuration XML files.

Hibernate invokes Class.isAssignableFrom(), and if false is returned, it exits silently, thus breaking the connection. I will open a ticket for Hibernate developers on this matter.

Comments