2012-12-03 21 views
1

我有1-1映射:SQLGrammarException:否參數1指定的值冬眠

@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "messageDetails") 
public MessageEntry getMessageEntry() { 
    return messageEntry; 
} 

@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) 
public MessageDetails getMessageDetails() { 
    return messageDetails; 
} 

我想刪除一些messageDetails:

 if (entries.size()>0) 
     sess.createQuery(
       "DELETE MessageDetails d " + 
       "WHERE d.messageEntry IN (:entries)") 
       .setParameterList("entries",entries).executeUpdate(); 

其中的條目是列表。

我得到這個SQL日誌中:

19:48:13,594 DEBUG SQL:104 - delete from MessageDetails where id in (? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , 
? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , 
? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , 
? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , 
? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , 
? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , 
? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , 
? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , 
? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , 
? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ?) 

這個堆棧跟蹤:

Exception in thread "main" org.hibernate.exception.SQLGrammarException: No value specified for parameter 1 
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122) 
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) 
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) 
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110) 
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129) 
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81) 
    at $Proxy18.executeUpdate(Unknown Source) 
    at org.hibernate.hql.internal.ast.exec.BasicExecutor.execute(BasicExecutor.java:95) 
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:413) 
    at org.hibernate.engine.query.spi.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:282) 
    at org.hibernate.internal.StatelessSessionImpl.executeUpdate(StatelessSessionImpl.java:420) 
    at org.hibernate.internal.QueryImpl.executeUpdate(QueryImpl.java:116) 
    at org.kriyak.hbm.Archive.updateFilesStateless(Archive.java:228) 
    at org.kriyak.parser.IndexArchiveFast.main(IndexArchiveFast.java:28) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) 
Caused by: java.sql.SQLException: No value specified for parameter 1 

我使用無狀態的會話。

回答

1

我通過加入集合的查詢,然後在聯接的列操作解決了這個問題的另一種方法簽名:

SELECT a FROM Anything a JOIN a.thisIsACollection coll WHERE coll IN :param 
0

我不認爲hibernate會自動將集合轉換爲以逗號分隔的參數值。

sess.createQuery(
       "DELETE MessageDetails d " + 
       "WHERE d.messageEntry IN (:entries)") 
       .setParameterList("entries",entries).executeUpdate(); 

對於條目,我建議將它轉換爲逗號分隔值或對批處理模式中的每個條目使用單獨的刪除語句。

+0

這個構造在其他精選案例中爲我工作。以下是手動文本的一部分:將多個值綁定到指定的查詢參數。首先通過查詢中的用法/位置來檢測參數的Hibernate類型,並且如果第二次從集合中的第一個對象的類別中猜測不足。這對於在(:value_list)中將值列表綁定到表達式(如foo.bar)很有用。 –

0

嘗試使用其中u通過集合對象類型作爲參數

setParameterList(String name, Collection vals, Type type)