2012-05-27 57 views
0

我的hibernate.cfg.xml是這樣的:錯誤在Hibernate查詢

<?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。謝謝您的幫助。

+0

我在這裏看到的空間「org.hibernate.hql.classic.ClassicQueryTranslatorFactor Y」ü可以檢查一次.. –

回答

1

您的實體班名爲MisurazioneOraria,而不是misurazione_oraria。 HQL使用實體和字段/屬性。它不使用表和列名稱。

閱讀HQL documentation

此外,Query.list()返回一個列表,而不一定是一個ArrayList。您不應該將查詢的結果強制轉換爲ArrayList。 !

+0

由於現在它的工作原理:d – blink89

0

將其更改爲

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