【问题标题】:Hibernate Mapping Could not extract ResultSetHibernate 映射无法提取 ResultSet
【发布时间】:2019-02-15 14:11:26
【问题描述】:

我正在尝试使用 Hibernate 从 Postgres DB 获取我的实体类的列表。似乎配置中的一切都是正确的,并且所有休眠属性对我来说都很好。

当我调用 findAll 方法时,我得到一个 org.hibernate.exception.SQLGrammarException: could not extract ResultSet

这是我的文件:

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="dialect">org.hibernate.dialect.PostgreSQL82Dialect</property>
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://localhost:5433/EES_Settings</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">postgres</property>
        <property name="current_session_context_class">thread</property>
        <property name="hibernate.show_sql">true</property>

        <!-- C3P0 connection pool -->
        <property name="hibernate.c3p0.timeout">600</property>
        <property name="hibernate.c3p0.maxIdleTimeExcessConnections">20</property>

        <!--  Connection testing settings -->
        <property name="hibernate.c3p0.validate">false</property>
        <property name="hibernate.c3p0.idle_test_period">30</property>
        <property name="hibernate.c3p0.automaticTestTable">conTestTable</property>

        <mapping class="com.deere.isg.easyequipmentsetup.easyequipmentsetup.entities.Configuration" resource="Mappers/Configuration.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

@Id
@Column(name = "guid")
private String guid;

@Column(name = "profile_name")
private String profileName;

@Column(name= "machine_id")
private String machineId;

@Column(name= "implement_id")
private String implementId;

@Column(name= "creation_date")
@Type(type="timestamp")
private Timestamp creationDate;

@Column(name= "modification_date")
@Type(type="timestamp")
private Timestamp modificationDate;
List<Configuration> configurations = (List<Configuration>) getCurrentSession().createSQLQuery("select Cast(guid as varchar) guid, profile_name, machine_id, implement_id, creation_data, modification_date from ees_configuration").list();

预期结果是数据库中记录的集合,实际结果是提到的异常。

【问题讨论】:

  • 始终分享完整的 stactrace
  • 为什么你使用原生 SQL 而不是 HQL?

标签: java postgresql hibernate


【解决方案1】:

尝试使用标准和方法列表来检索为实体存储的所有记录:

List<Configuration> configurations = getCurrentSession().createCriteria(Configuration.class).list();

如果您使用 ORM(在您的情况下为 Hibernate),不建议使用 sql 查询。

如果您更喜欢使用 Sql 查询并仅提取 bean 的某些列,请使用 addScalar 将所有列绑定到您需要的类型

【讨论】:

  • 请勿使用Try to do this: 回答。解释你做了什么以及为什么
猜你喜欢
  • 2015-05-27
  • 1970-01-01
  • 2014-07-20
  • 2016-05-06
  • 2016-02-17
  • 2017-11-22
  • 2015-08-03
  • 2016-09-28
相关资源
最近更新 更多