Hibernate 3:无法查询PostgreSQL数据库
-
01-07-2019 - |
题
我正在使用 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=?
并在psql中的数据库中再次执行它,它可以工作(这意味着Hibernate生成了一个正确的SQL)。
以下是Hibernate配置:
<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>
搜索意外的EOF
日志条目并不是很好。任何想法,社区?
解决方案
将调试器应用于Hibernate代码后,它已修复!
在问题的文本中看不到它,但问题是传递给 createCriteria()
方法的 Place
来自另一个包,而不是 com /示例/数据
,在配置XML文件中指定。
Hibernate调用 Class.isAssignableFrom()
,如果返回false,则以静默方式退出,从而断开连接。我将就此问题为Hibernate开发人员打开一张票。
不隶属于 StackOverflow