我正在使用 Hibernate 3.3.1 GA
和 PostgreSQL 8.3
建立一个项目。我刚刚创建了一个数据库,第一个表,在那里添加了一行,现在正在配置 Hibernate。
然而,即使是最简单的查询:
Criteria criteria = session.createCriteria(Place.class);
List result = criteria.list();
无法执行(数据库中只有一条记录,但返回空列表)。我查看了 PostgreSQL 日志:
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
我使用纯 JDBC 编写了一个简单的程序来获取相同的数据并且它运行良好。本例中的 PostgreSQL 日志如下所示(用于比较):
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 调试日志没有指示任何错误。如果我接受日志中列出的查询:
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>
...和映射文件:
<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>
用谷歌搜索 unexpected EOF
日志条目并不顺利。任何想法,社区?
最佳答案
对Hibernate代码应用调试器后,它是固定的!
它在问题的文本中不可见,但问题是传递给 createCriteria()
方法的 Place
来自另一个包,而不是 com/example/data
,在配置 XML 文件中指定。
Hibernate 调用 Class.isAssignableFrom()
,如果返回 false,它会静默退出,从而中断连接。我将就此事为 Hibernate 开发人员开票。
关于java - hibernate 3 : unable to query PostgreSQL database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/108171/