0
@NamedQueries({
    @NamedQuery(name="surveyResponse.getAverageRatings", query="SELECT AVG(ratings) FROM SurveyResponse surveyResponse WHERE surveyResponse.customerSurvey.surveyId = :surveyId"),
    @NamedQuery(name="surveyResponse.getBySurvey",  query = "SELECT FROM SurveyResponse surveyResponse WHERE surveyResponse.customerSurvey.surveyId = :surveyId")})
public class SurveyResponse implements Serializable {
    @Id @GeneratedValue(strategy=GenerationType.SEQUENCE,  
    generator="SURVEY_RESPONSE_SEQUENCE") 
    @Column(name="SURVEYRESPONSEID")
    private long surveyResponseId; 

    @ManyToOne(fetch=FetchType.EAGER, cascade=CascadeType.DETACH)
    @JoinColumn(name="QNo", referencedColumnName="qNo")
    private Questionair questionair;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="SURVEYID", referencedColumnName="surveyId")
    private CustomerSurvey customerSurvey;

    @Column(name="RATINGS")
    private int ratings;

    .....
}

持久性类:

 public void submitSurveyResponse(List<CustomerSurvey> surveys) throws WorkServiceException {
    EntityManager em = entityManagerFactory.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();

    for (CustomerSurvey survey : surveys) {
        survey.setRespondedOn(Calendar.getInstance().getTime());
        String surveyId = survey.getSurveyId();
        Query query = em.createNamedQuery("surveyResponse.getAverageRatings").setParameter("surveyId", surveyId);

        Double avgRating = (Double) query.getSingleResult();
        BigDecimal bd = BigDecimal.valueOf(avgRating);
        bd = bd.setScale(2, RoundingMode.HALF_UP);
        avgRating = bd.doubleValue();

        survey.setAvgRatings(avgRating);
        CustomerSurvey mSurvey = em.merge(survey);
        em.flush();
    }
    tx.commit();
    em.clear();
    em.close();
}

原因 :

2012-10-24 11:33:57,042[SpringOsgiExtenderThread-34]|DEBUG|DataNucleus.Transaction|147-org.datanucleus-3.1.1|Object "com.ge.dsp.iwork.entity.SurveyResponse@3a45cd18" (id="com.ge.dsp.iwork.entity.SurveyResponse:290") being evicted from transactional cache
2012-10-24 11:33:57,043[SpringOsgiExtenderThread-34]|DEBUG|DataNucleus.Transaction|147-org.datanucleus-3.1.1|Object "com.ge.dsp.iwork.entity.SurveyResponse@3a45cd18" (id="com.ge.dsp.iwork.entity.SurveyResponse:290") is not transactional
2012-10-24 11:33:57,043[SpringOsgiExtenderThread-34]|DEBUG|DataNucleus.Datastore|147-org.datanucleus-3.1.1|Closing PreparedStatement "org.datanucleus.store.rdbms.ParamLoggingPreparedStatement@3c9c008b"
2012-10-24 11:33:57,100[SpringOsgiExtenderThread-34]|DEBUG|DataNucleus.Transaction|147-org.datanucleus-3.1.1|Transaction created [DataNucleus Transaction, ID=Xid=   ', enlisted resources=[]]
2012-10-24 11:33:57,101[SpringOsgiExtenderThread-34]|DEBUG|DataNucleus.Transaction|147-org.datanucleus-3.1.1|Transaction begun for ObjectManager org.datanucleus.ObjectManagerImpl@1326f1a6 (optimistic=true)
2012-10-24 11:33:57,104[SpringOsgiExtenderThread-34]|DEBUG|com.ge.dsp.kernel.bootloader.impl.DspServiceListener|153-dsp.kernel-0.8.0|MODIFIED
2012-10-24 11:33:57,104[SpringOsgiExtenderThread-34]|DEBUG|com.ge.dsp.kernel.bootloader.impl.DspServiceListener|153-dsp.kernel-0.8.0|ServiceReference [cxf-dosgi-ri-topology-manager] {org.osgi.service.remoteserviceadmin.EndpointListener}={endpoint.listener.scope=[(&(objectClass=org.apache.aries.blueprint.NamespaceHandler)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.aries.proxy.ProxyManager)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=javax.management.MBeanServer)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.karaf.features.FeaturesService)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.karaf.admin.AdminService)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.karaf.jaas.config.JaasRealm)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.karaf.jaas.config.KeystoreInstance)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.karaf.diagnostic.core.DumpProvider)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.karaf.features.FeaturesListener)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.karaf.jaas.config.KeystoreManager)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.karaf.jaas.modules.BackingEngineFactory)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.geronimo.osgi.registry.api.ProviderRegistry)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=javax.persistence.spi.PersistenceProvider)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.karaf.shell.commands.info.InfoProvider)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=com.ge.dsp.kernel.spi.DspServiceManagerService)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(&(component=jetty)(objectClass=org.apache.camel.spi.ComponentResolver))(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(&(component=bean)(objectClass=org.apache.camel.spi.ComponentResolver))(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(&(component=direct)(objectClass=org.apache.camel.spi.ComponentResolver))(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(&(component=log)(objectClass=org.apache.camel.spi.ComponentResolver))(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(&(language=simple)(objectClass=org.apache.camel.spi.LanguageResolver))(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.camel.spi.TypeConverterLoader)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=com.ge.dsp.kernel.spi.DspKernelPluggable)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=com.ge.dsp.dsi.clustering.DspClusterManager)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=com.ge.dsp.kernel.spi.DspWebServiceEnabler)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=com.ge.dsp.dsi.config.ConfigurationServiceFactory)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44)))],service.id=127}
2012-10-24 11:33:57,104[SpringOsgiExtenderThread-34]|DEBUG|com.ge.dsp.kernel.bootloader.impl.DspServiceListener|153-dsp.kernel-0.8.0|Found activities of NON-DSP services: ServiceReference [cxf-dosgi-ri-topology-manager] {org.osgi.service.remoteserviceadmin.EndpointListener}={endpoint.listener.scope=[(&(objectClass=org.apache.aries.blueprint.NamespaceHandler)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.aries.proxy.ProxyManager)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=javax.management.MBeanServer)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.karaf.features.FeaturesService)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.karaf.admin.AdminService)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.karaf.jaas.config.JaasRealm)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.karaf.jaas.config.KeystoreInstance)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.karaf.diagnostic.core.DumpProvider)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.karaf.features.FeaturesListener)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.karaf.jaas.config.KeystoreManager)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.karaf.jaas.modules.BackingEngineFactory)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.geronimo.osgi.registry.api.ProviderRegistry)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=javax.persistence.spi.PersistenceProvider)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.karaf.shell.commands.info.InfoProvider)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=com.ge.dsp.kernel.spi.DspServiceManagerService)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(&(component=jetty)(objectClass=org.apache.camel.spi.ComponentResolver))(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(&(component=bean)(objectClass=org.apache.camel.spi.ComponentResolver))(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(&(component=direct)(objectClass=org.apache.camel.spi.ComponentResolver))(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(&(component=log)(objectClass=org.apache.camel.spi.ComponentResolver))(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(&(language=simple)(objectClass=org.apache.camel.spi.LanguageResolver))(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=org.apache.camel.spi.TypeConverterLoader)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=com.ge.dsp.kernel.spi.DspKernelPluggable)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=com.ge.dsp.dsi.clustering.DspClusterManager)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=com.ge.dsp.kernel.spi.DspWebServiceEnabler)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44))), (&(objectClass=com.ge.dsp.dsi.config.ConfigurationServiceFactory)(!(endpoint.framework.uuid=eae78622-cdca-4196-ab13-f298ac516e44)))],service.id=127}
2012-10-24 11:33:57,177[SpringOsgiExtenderThread-34]|ERROR|org.springframework.osgi.extender.internal.activator.ContextLoaderListener|61-org.springframework.osgi.extender-1.2.1|Application context refresh failed (OsgiBundleXmlApplicationContext(bundle=dsp.dsp-iwork, config=osgibundle:/META-INF/spring/*.xml))
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testSubmitSurveyResponse' defined in URL [bundle://170.45:0/META-INF/spring/module-context.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Query has parameter "surveyId" supplied at execution yet the query doesnt make use of this parameter
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)[54:org.springframework.beans:3.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)[54:org.springframework.beans:3.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)[54:org.springframework.beans:3.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)[54:org.springframework.beans:3.1.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)[54:org.springframework.beans:3.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)[54:org.springframework.beans:3.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)[54:org.springframework.beans:3.1.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)[54:org.springframework.beans:3.1.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)[56:org.springframework.context:3.1.2.RELEASE]
    at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.access$1600(AbstractDelegatedExecutionApplicationContext.java:69)[60:org.springframework.osgi.core:1.2.1]
    at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext$4.run(AbstractDelegatedExecutionApplicationContext.java:355)[60:org.springframework.osgi.core:1.2.1]
    at org.springframework.osgi.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85)[60:org.springframework.osgi.core:1.2.1]
    at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:320)[60:org.springframework.osgi.core:1.2.1]
    at org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:132)[61:org.springframework.osgi.extender:1.2.1]
    at java.lang.Thread.run(Thread.java:662)[:1.6.0_34]
Caused by: java.lang.IllegalArgumentException: Query has parameter "surveyId" supplied at execution yet the query doesnt make use of this parameter
    at org.datanucleus.api.jpa.JPAQuery.setParameter(JPAQuery.java:440)
    at org.datanucleus.api.jpa.JPAQuery.setParameter(JPAQuery.java:56)
    at com.ge.dsp.iwork.persistence.JpaWorkPersistence.submitSurveyResponse(JpaWorkPersistence.java:402)
    at com.ge.dsp.iwork.impl.WorkServiceImpl.submitSurveyResponse(WorkServiceImpl.java:239)
    at com.ge.dsp.iwork.test.WorkServiceTest.testSubmitSurveyResponse(WorkServiceTest.java:264)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.6.0_34]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)[:1.6.0_34]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)[:1.6.0_34]
    at java.lang.reflect.Method.invoke(Method.java:597)[:1.6.0_34]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1581)[54:org.springframework.beans:3.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1522)[54:org.springframework.beans:3.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)[54:org.springframework.beans:3.1.2.RELEASE]
    ... 14 more
Caused by: org.datanucleus.store.query.QueryInvalidParametersException: Query has parameter "surveyId" supplied at execution yet the query doesnt make use of this parameter
    at org.datanucleus.store.query.Query.applyImplicitParameterValueToCompilation(Query.java:881)
    at org.datanucleus.store.query.AbstractJPQLQuery.compileInternal(AbstractJPQLQuery.java:310)
    at org.datanucleus.store.rdbms.query.JPQLQuery.compileInternal(JPQLQuery.java:202)
    at org.datanucleus.store.query.Query.setImplicitParameter(Query.java:799)
    at org.datanucleus.store.rdbms.query.JPQLQuery.setImplicitParameter(JPQLQuery.java:147)
    at org.datanucleus.api.jpa.JPAQuery.setParameter(JPAQuery.java:436)
    ... 25 more
4

1 回答 1

1

我想,没关系。但有一件事referencedColumnNameCustomerSurvey

更改referencedColumnName="surveyId"referencedColumnName="SERVEYID"。使用大写字母,因为您在CustomerSurvey实体(Previous Post)中使用大写字母。

你可以参考这里

更新答案:

当我检查您的查询surveyResponse.getAverageRatings时,缺少AVG(ratings). 我认为,应该AVG(surveyResponse.ratings)

SELECT AVG(surveyResponse.ratings) FROM SurveyResponse surveyResponse WHERE surveyResponse.customerSurvey.surveyId = :surveyId
于 2012-10-24T04:04:47.107 回答