休眠3:无法查询PostgreSQL数据库
时间:2020-03-06 14:29:03 来源:igfitidea点击:
我正在使用" 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
来自配置XML文件中指定的另一个包,而不是com / example / data
。
Hibernate调用Class.isAssignableFrom()
,如果返回false,它会静默退出,从而断开连接。我将为此问题向Hibernate开发人员开放一张票。