Hibernate 3:无法查询 PostgreSQL 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/108171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate 3: unable to query PostgreSQL database
提问by Sergey Mikhanov
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.
我正在使用Hibernate 3.3.1 GA
和建立一个项目PostgreSQL 8.3
。我刚刚创建了一个数据库,第一个表,在那里添加了一行,现在正在配置 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:
无法执行(尽管数据库中有一条记录,但返回空列表)。我查看了 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
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):
我使用普通 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 debug log does not indicate any errors. If I take the query listed in the logs:
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).
并在 psql 中的数据库中再次执行它,它可以工作(这意味着 Hibernate 已经生成了正确的 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?
谷歌搜索unexpected EOF
日志条目并不费力。任何想法,社区?
采纳答案by Sergey Mikhanov
After applying debugger to the Hibernate code, it is fixed!
将调试器应用到 Hibernate 代码后,就修复了!
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.
它在问题的文本中不可见,但问题是Place
传递给createCriteria()
方法的是来自另一个包,而不是com/example/data
配置 XML 文件中指定的 。
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.
Hibernate 调用Class.isAssignableFrom()
,如果返回 false ,它会静默退出,从而断开连接。我会在这件事上为 Hibernate 开发者开一张票。