简体   繁体   中英

SQLGrammarException: No value specified for parameter 1 hibernate

I have 1-1 mapping:

@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;
}

I want to delete some messageDetails:

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

where entries is List.

I get this SQL in logs:

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

and this 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

I use stateless session.

我通过将集合连接到查询,然后对连接的列进行操作来解决这个问题:

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

i don't think that hibernate will automatically convert collection to comma separated parameter values.

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

for entries i would suggest to convert it to comma separated values or use seperate delete statement for each entries in batch mode.

尝试使用其他方法签名,其中您将集合对象类型作为参数传递

setParameterList(String name, Collection vals, Type type)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM