我有2个实体:UserUserWithPasswordUserWithPassword包含User
我定义了2个匹配的存储库:UserRepositoryUserWithPasswordRepository,都扩展了CrudRepository

我向UserWithPasswordRepository添加了一个额外的查找器:

UserWithPassword findByUser( User user );


但这给出了以下例外:

No value specified for parameter 1


完整跟踪:

2015-01-21 16:07:36 DEBUG [http-bio-8080-exec-8] SharedEntityManagerCreator$SharedEntityManagerInvocationHandler - Creating new EntityManager for shared EntityManager invocation
2015-01-21 16:07:36 DEBUG [http-bio-8080-exec-8] SQL - select userwithpa0_.id as id1_55_, userwithpa0_.password as password2_55_ from UserWithPassword userwithpa0_ where userwithpa0_.id=?
2015-01-21 16:07:36 DEBUG [http-bio-8080-exec-8] LazyConnectionDataSourceProxy - Connecting to database for operation 'prepareStatement'
2015-01-21 16:07:36 WARN [http-bio-8080-exec-8] SqlExceptionHelper - SQL Error: 0, SQLState: 07001
2015-01-21 16:07:36 ERROR [http-bio-8080-exec-8] SqlExceptionHelper - No value specified for parameter 1
2015-01-21 16:07:36 DEBUG [http-bio-8080-exec-8] LazyConnectionDataSourceProxy - Using existing database connection for operation 'getAutoCommit'
2015-01-21 16:07:36 DEBUG [http-bio-8080-exec-8] EntityManagerFactoryUtils - Closing JPA EntityManager
2015-01-21 16:07:36 DEBUG [http-bio-8080-exec-8] LazyConnectionDataSourceProxy - Using existing database connection for operation 'isClosed'
2015-01-21 16:07:36 DEBUG [http-bio-8080-exec-8] LazyConnectionDataSourceProxy - Using existing database connection for operation 'getWarnings'
2015-01-21 16:07:36 DEBUG [http-bio-8080-exec-8] LazyConnectionDataSourceProxy - Using existing database connection for operation 'clearWarnings'
2015-01-21 16:07:36 DEBUG [http-bio-8080-exec-8] LazyConnectionDataSourceProxy - Using existing database connection for operation 'close'
2015-01-21 16:07:36 ERROR [http-bio-8080-exec-8] BlazeDSLogging - Error processing remote invocation: org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
  incomingMessage: Flex Message (flex.messaging.messages.RemotingMessage)
    operation = login
    clientId = 3ABCA24D-2F09-7837-5996-E4B98C60D8E9
    destination = authenticationClientService
    messageId = E0376B16-2B58-E7A0-4F58-0D08CEA2927B
    timestamp = 1421852855995
    timeToLive = 0
    body =
    [
      admin,
      21232F297A57A5A743894A0E4A801FC3
    ]
    hdr(DSId) = 3ABBA890-141C-5F5D-DF87-EC302DA0E085
    hdr(DSEndpoint) = channel-amf
org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:231)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:214)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:122)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at com.sun.proxy.$Proxy46.findByUser(Unknown Source)
    at com.traficon.tmsng.server.web.service.impl.UserServiceImpl.canLogin(UserServiceImpl.java:300)
    at com.traficon.tmsng.server.web.service.impl.AuthenticationServiceImpl.login(AuthenticationServiceImpl.java:95)
    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 flex.messaging.services.remoting.adapters.JavaAdapter.invoke(JavaAdapter.java:418)
    at flex.messaging.services.RemotingService.serviceMessage(RemotingService.java:183)
    at flex.messaging.MessageBroker.routeMessageToService(MessageBroker.java:1400)
    at flex.messaging.endpoints.AbstractEndpoint.serviceMessage(AbstractEndpoint.java:1005)
    at flex.messaging.endpoints.AbstractEndpoint$$FastClassBySpringCGLIB$$1a3ef066.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.flex.core.MessageInterceptionAdvice.invoke(MessageInterceptionAdvice.java:66)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:125)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$FixedChainStaticTargetInterceptor.intercept(CglibAopProxy.java:603)
    at flex.messaging.endpoints.AMFEndpoint$$EnhancerBySpringCGLIB$$47abc88f.serviceMessage(<generated>)
    at flex.messaging.endpoints.amf.MessageBrokerFilter.invoke(MessageBrokerFilter.java:103)
    at flex.messaging.endpoints.amf.LegacyFilter.invoke(LegacyFilter.java:158)
    at flex.messaging.endpoints.amf.SessionFilter.invoke(SessionFilter.java:44)
    at flex.messaging.endpoints.amf.BatchProcessFilter.invoke(BatchProcessFilter.java:67)
    at flex.messaging.endpoints.amf.SerializationFilter.invoke(SerializationFilter.java:166)
    at flex.messaging.endpoints.BaseHTTPEndpoint.service(BaseHTTPEndpoint.java:291)
    at flex.messaging.endpoints.AMFEndpoint$$EnhancerBySpringCGLIB$$47abc88f.service(<generated>)
    at org.springframework.flex.servlet.MessageBrokerHandlerAdapter.handle(MessageBrokerHandlerAdapter.java:109)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:695)
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:124)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:88)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:2062)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1859)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)
    at org.hibernate.loader.Loader.doQuery(Loader.java:906)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:348)
    at org.hibernate.loader.Loader.doList(Loader.java:2550)
    at org.hibernate.loader.Loader.doList(Loader.java:2536)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2366)
    at org.hibernate.loader.Loader.list(Loader.java:2361)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:495)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:357)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:198)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1194)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
    at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:287)
    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 org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:362)
    at com.sun.proxy.$Proxy96.getSingleResult(Unknown Source)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:197)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:74)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:98)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:89)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:421)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:381)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:267)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
    ... 59 more
Caused by: java.sql.SQLException: No value specified for parameter 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
    at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2611)
    at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2586)
    at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2510)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2259)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79)
    ... 92 more


我也尝试过这个:

@Query("from UserWithPassword where user=:user")
UserWithPassword findByUser( @Param("user") User user );


但是结果是一样的。

这是我的.hbm.xml文件中的内容:

对于用户:

<class name="User" table="User">
        <id name="id" type="int" column="id" unsaved-value="-1">
            <generator class="native"/>
        </id>

        <property name="userName" column="userName" not-null="true" unique="true" />
        <property name="language" column="language"/>
        <property name="active" column="active" not-null="true"/>
        <property name="unit" column="unit">
            <type name="com.traficon.tmsng.server.common.service.persistence.impl.hibernate.usertype.EnumUserType">
                <param name="enumClass">com.traficon.domain.system.UnitType</param>
            </type>
        </property>
        <property name="commentUsage" column="commentUsage" not-null="true">
            <type name="com.traficon.tmsng.server.common.service.persistence.impl.hibernate.usertype.EnumUserType">
                <param name="enumClass">com.traficon.domain.authentication.CommentUsageType</param>
            </type>
        </property>
        <property name="passwordPresent" column="passwordPresent" not-null="true" />
        <property name="showMapsInRealTime" column="showMaps" not-null="true">
            <type name="com.traficon.tmsng.server.common.service.persistence.impl.hibernate.usertype.EnumUserType">
                <param name="enumClass">com.traficon.domain.authentication.ShowMapsInRealTimeOption</param>
            </type>
        </property>
        <property name="maxNumberOfRealtimeVideoPopups" column="maxNrRealtimeVideoPopups" not-null="true"/>
        <property name="eventTypeSelectionLocked" column="eventTypeSelectionLocked"/>

        <many-to-one name="role" column="role" class="Role" not-null="true" lazy="false" />
    </class>


对于UserWithPassword:

<class name="UserWithPassword" table="UserWithPassword">
        <id name="id" type="int" column="id" unsaved-value="-1" >
            <generator class="foreign">
                <param name="property">user</param>
            </generator>
        </id>

        <one-to-one name="user" class="User" constrained="true" lazy="false" cascade="all"/>
        <property name="password" column="password"/>
    </class>


如果启用了Hibernate SQL日志记录,则会看到以下查询:

select userwithpa0_.id as id1_55_, userwithpa0_.password as password2_55_ from UserWithPassword userwithpa0_ where userwithpa0_.id=?


似乎没问题,除了问号没有任何约束。我将org.hibernate.type设置为TRACE并且没有打印任何有关参数的内容,而我确实看到了其他语句的这种日志记录。例如:

2015-01-21 16:07:35 DEBUG [http-bio-8080-exec-8] SQL - select user0_.id as id1_54_, user0_.userName as userName2_54_, user0_.language as language3_54_, user0_.active as active4_54_, user0_.unit as unit5_54_, user0_.commentUsage as commentU6_54_, user0_.passwordPresent as password7_54_, user0_.showMaps as showMaps8_54_, user0_.maxNrRealtimeVideoPopups as maxNrRea9_54_, user0_.eventTypeSelectionLocked as eventTy10_54_, user0_.role as role11_54_ from User user0_ where user0_.userName=?
2015-01-21 16:07:35 DEBUG [http-bio-8080-exec-8] LazyConnectionDataSourceProxy - Connecting to database for operation 'prepareStatement'
2015-01-21 16:07:36 TRACE [http-bio-8080-exec-8] BasicBinder - binding parameter [1] as [VARCHAR] - admin


是此查询的输出(在UserRepository中):

用户findByUserName(String userName);

我究竟做错了什么?如果查询参数不是“简单”类型,是否需要做一些特别的事情?

最佳答案

请检查您是否没有错过。

如果您没有在查询中获取用户对象,则可能是因为您没有以EAGER方式获取UserWithPassword。延迟获取对象会为内部对象创建一个代理对象,因此您将无法获得User对象的值。

如果我没有记错的话,我希望默认的提取类型为LAZY

10-06 04:57