我正在尝试使用Hibernate从Postgres DB中获取我的实体类的列表。似乎一切在配置中都是正确的,并且所有的休眠属性对我来说都很不错。
当我调用findAll方法时,我得到一个org.hibernate.exception.SQLGrammarException:无法提取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();
预期结果是带有数据库记录的集合,实际结果是提到的异常。
答案 0 :(得分:0)
尝试使用“条件和方法”列表来检索为该实体存储的所有记录:
List<Configuration> configurations = getCurrentSession().createCriteria(Configuration.class).list();
如果您使用ORM(在您的情况下为休眠),则不建议使用sql查询。
或者,如果您更喜欢使用Sql查询并仅提取bean的某些列,请使用addScalar
将所有列绑定为所需的类型