mysql - hibernate 查询错误

标签 mysql hibernate

我的 hibernate.cfg.xml 是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<!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="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/****</property>
<property name="hibernate.connection.username">***</property>
<property name="hibernate.connection.password">*****</property>

<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactor    y</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">false</property>
<property name="hbm2ddl.auto">create</property>
<!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>
<!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

<mapping class="entities.MisurazioneOraria"/>
<mapping class=....
    ....>
<listener type="post-insert" class="MisurazioneOrariaInsertListener"/>

</session-factory>

</hibernate-configuration>

我尝试从作为监听器的这一行代码进行查询:

ArrayList<MisurazioneOraria> mis =  (ArrayList<MisurazioneOraria>)    session.createQuery("from (misurazione_oraria) where sensore_idsensore = :idsensore").setParameter("idsensore", m.getSensore().getIdSensore()).list();

错误是:

mag 27, 2012 6:30:33 PM org.hibernate.event.def.AbstractFlushingEventListener   performExecutions
Grave: Could not synchronize database state with session
org.hibernate.QueryException: in expected: misurazione_oraria [from (misurazione_oraria) where sensore_idsensore = :idsensore]

我的mysql版本是5.5.23。感谢您的帮助。

最佳答案

您的实体类名为 MisurazioneOraria,而不是 misurazione_oraria。 HQL 使用实体和字段/属性。它不使用表名和列名。

阅读HQL documentation .

此外,Query.list() 返回一个 List,而不一定是 ArrayList。您不应该将查询结果转换为 ArrayList。

关于mysql - hibernate 查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10775792/

相关文章:

mysql - 无法将数据传输到 mySQL 数据库

mysql - 命令不同步;您现在无法运行此命令 - EXT :indexed_search 的 TYPO3 后端模块上出现错误

c# - MySql语句准备 "not sticking"

java - 使用带参数的 HQL 选择表行

performance - Grails 外键更新 - 性能更新对象

php - 没有收到要导入的数据

mysql - 复杂的Mysql查询分组列

java - Spring JPA ManyToOne - 在刷新之前保存 transient 实例

java - H2 的 Hibernate Multi-Tenancy 问题 : wrong schema

java - 具有多级连接的 Hibernate Criteria