Java SQLGrammarException:没有为参数1 hibernate指定值
我有1-1映射:Java SQLGrammarException:没有为参数1 hibernate指定值,java,mysql,hibernate,hql,Java,Mysql,Hibernate,Hql,我有1-1映射: @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "messageDetails") public MessageEntry getMessageEntry() { return messageEntry; } @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) public MessageDetails
@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 (? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ,
? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ,
? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ,
? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ,
? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ,
? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ,
? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ,
? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ,
? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ,
? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ?)
这是stacktrace:
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
我使用无状态会话。我认为hibernate不会自动将集合转换为逗号分隔的参数值
sess.createQuery(
"DELETE MessageDetails d " +
"WHERE d.messageEntry IN (:entries)")
.setParameterList("entries",entries).executeUpdate();
对于条目,我建议将其转换为逗号分隔的值,或者在批处理模式下对每个条目使用separatedelete语句 尝试使用另一个方法签名,其中您将collections对象类型作为参数传递
setParameterList(String name, Collection vals, Type type)
通过将集合连接到查询,然后对连接的列进行操作,我解决了这个问题:
SELECT a FROM Anything a JOIN a.thisIsACollection coll WHERE coll IN :param
这个结构在其他一些特定的情况下对我有效。以下是手册中的部分文本:将多个值绑定到命名查询参数。首先通过查询中的用法/位置检测参数的Hibernate类型,如果不够,则从集合中第一个对象的类中猜测参数的Hibernate类型。这对于将值列表绑定到表达式(如(:value\u list)中的foo.bar)非常有用。