After creating a stored procedure query, I later need to access the procedure name starting from the StoredProcedureQuery instance:
EntityManager em;
StoredProcedureQuery sp = em.createStoredProcedureQuery("sp_name");
// get sp_name from sp instance
// ?
Is this possible using the public methods of StoredProcedureQuery?
you could use the following
if (sp instanceof ProcedureCall) {
String procedureName = ((ProcedureCall) sp).getProcedureName();
}
I use spring data jpa, I have these classe (each one have id... not displayed)
public class HOV{
#ManyToOne(optional = false, fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
#JoinColumn(name = "vehicle_type_id")
private VehicleTypes vehicleType;
...
}
public class VehicleTypes{
#OneToMany(mappedBy = "vehicleType")
private List<Vehicles> vehicles = new ArrayList<>();
}
so in hov repository, i tried to search by vehiclesId
List<HOV> findByVehicleTypeVehiclesId(Integer id);
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'hOVRepository': Invocation of init
method failed; nested exception is java.lang.IllegalArgumentException:
Failed to create query for method public abstract java.util.List
com.lcm.repository.HOVRepository.findByVehicleTypeVehiclesId(java.lang.Integer)!
Illegal attempt to dereference path source [null.vehicleType.vehicles]
of basic type
In your situation, I think, is better to provide a query yourself, for example:
#Query("select h from HOV h join h.vehicleType vt left join vt.vehicles v where v.id = ?1")
List<HOV> findWithQuery(Integer vehicleId);
I am trying to return a string from a table based on a conditional ID (subid) from an already populated table. The query should return a list of type ItemDataPoint entity. In a JSF managed bean, the list will the be iterated by a an enhaned for loop. If the word "Include" is found by the loop, the method will create a specific type of chart. In simpler terms, I want to return a string based the ID condition being met. I am getting:
javax.ejb.EJBException
at com.sun.ejb.containers.EJBContainerTransactionManager.processSystemException
(EJBContainerTransactionManager.java:748)
at com.sun.ejb.containers.EJBContainerTransactionManager.
completeNewTx(EJBContainerTransactionManager.java:698)
at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx
(EJBContainerTransactionManager.java:503)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4475)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2009)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1979)
Caused by: java.lang.IllegalArgumentException: You have attempted to set
a parameter at position 2 which does not exist in this query string SELECT p FROM
Itemdatapoint p JOIN p.series s WHERE s.master.item.subs.subid = :subid.
at org.eclipse.persistence.internal.jpa.QueryImpl.setParameterInternal(QueryImpl.java:925)
at org.eclipse.persistence.internal.jpa.QueryImpl.setParameterInternal(QueryImpl.java:906)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.setParameter(EJBQueryImpl.java:469)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.setParameter(EJBQueryImpl.java:1)
at com.manaar.clientmods.gazprom.design3.data.facade.ItemdatapointFacade.
chartType(ItemdatapointFacade.java:78)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
The subid value of 2 exists if I run a native SQL query on the relevant parent table in the DB. Also the type of the subid is an int in the main entity class, the JPQL Facade class and the managed bean.
The JPQL:
public List<Itemdatapoint> chartType (int subid) {
Query q = em.createQuery("SELECT p FROM Itemdatapoint p JOIN p.series s WHERE s.master.item.subs.subid = :subid");
q.setParameter(subid, "subid");
return q.getResultList();
}
The managed bean:
#Named(value = "reportBean")
#SessionScoped
public class ReportBean implements Serializable {
#Inject
private ItemdatapointFacade facade;
public String typeSwitch1() {
subid = 2;
chartType = facade.chartType(subid);
for(Itemdatapoint e: chartType) {
status = e.getSeries().getMaster().getStatus();
if(status.equals("Include")) {
return "line";
}
}
return null;
}
The xhtml page:
<p:chart type="#{reportBean.typeSwitch1()}" model="#{reportBean.subLineChart1}"/>
I also tried a non Join JPQL just from a single table:
public List<Itemdatapoint> noJoin (int subid) {
Query q = em.createQuery("SELECT p FROM Itemdatapoint p WHERE p.pointid = :subid");
q.setParameter(subid, "subid");
return q.getResultList();
}
Similar problem:
java.lang.IllegalArgumentException: You have attempted to set a
parameter at position 2 which does not exist in this query string
SELECT p FROM Itemdatapoint p WHERE p.pointid = :subid.
I gather that IllegalArgumentException means that the selected entity does not exist or is not the correct type consistent with the query string in the facade class. But in my case the entity exists and the parameter is the correct type.
I would appreciate any help in understanding why i'm getting this error. Thank in advance!
UPDATE
Responding to the answer from lametaweb, I want to better understand the concept of JPA parameters.
According to the JPA documentation, the first argument of the setParameter method is the parameter name or number. The second argument is the object that should be bound to the named parameter. Why does the following work without throwing Illegal ArgumentException?
I tested an xhtml (web page):
<p:dataGrid id="rep1" columns="1" value="#{pageBean.itemPageList1}" var="items1" rows="4">
<p:commandLink value="#{items1.itemname}" action="#{pageBean.showItem1}" ajax="false"/>
</p:dataGrid>
The bean code:
public ListDataModel<Sectionitem> getItemPageList1() {
subid = 1;
reportStatus = "Include";
itemPageList1 = itemFacade.viewItems(subid, reportStatus);
return itemPageList1;
}
The JPA facade:
public ListDataModel<Sectionitem> viewItems(int subid, String stat) {
Query q = em.createQuery("select s from Sectionitem s JOIN s.subs c where c.subid = :subid AND s.status = :stat ORDER BY s.daterec");
q.setParameter("subid", subid);
q.setParameter("stat", stat);
ListDataModel<Sectionitem> res
= new ListDataModel<Sectionitem>(q.getResultList());
return res;
}
Why is it in this case, the object exist but in my original case the subid object does not exist?
You are invoking this method in your code:
setParameter(int position, Object value)
but you have to invoke this one instead:
setParameter(String name, Object value)
So your code should be:
q.setParameter("subid", Integer.valueOf(subid));
But, if you invoke:
q.setParameter(subid, "subid");
here the first parameter represents the position of the argument and the second the value for it. So you are passing a value of "subid" for the parameter in the second (2) position, which doesn't exist, because you only have one parameter in your JPQL query, hence the IllegalArgumentException exception.
Note: Why do you have a primitive type in your entity? Why not an Integer instead an int?
#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;
.....
}
persistence class:
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();
}
Cause :
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
I think, it is OK. But the one thing is referencedColumnName of CustomerSurvey.
Change to referencedColumnName="surveyId" to referencedColumnName="SERVEYID". Use uppercase letter because of you use the uppercase letter in CustomerSurvey entity(Previous Post).
You can reference here.
Update Answer :
When I check your query surveyResponse.getAverageRatings, there is a missing AVG(ratings). I think, it should AVG(surveyResponse.ratings).
SELECT AVG(surveyResponse.ratings) FROM SurveyResponse surveyResponse WHERE surveyResponse.customerSurvey.surveyId = :surveyId
I am trying the following which is resulting in an additional update execution and failing my tests.
I have an entity like this.
#Entity
#SqlResultSetMapping(name = "tempfilenameRSMapping",
entities = { #EntityResult(entityClass = MyEntity.class) },
columns = { #ColumnResult(name = "TEMPFILENAME") })
//The reason for this mapping is to fetch an additional field data through join.
#Table(name = "MY_TABLE")
public class MyEntity {
#Id
#Column(name="ID")
private String id;
#Column(name="NAME")
private String name;
#Column(name="DESC")
private String description;
#Column(name="STATUS")
private String status;
//follwed by getter setters
}
I am trying to do a retrieve with a native query. And for the retrieved entity, I execute a native update (the reason for native update is that I want to update just one single field). Note that I am not updating the retrieved entity directly.
What I observe is that my update is not getting executed properly. When I turn the TRACE on, I notice that on flush openJPA is executing an additional update query and therefore overriding my original update.
e.g.
SELECT M.ID, M.NAME, M.DESC, O.TEMPFILENAME FROM MY_TABLE M, OTHER_TABLE O WHERE M.ID = ?
UPDATE MY_TABLE SET STATUS = ? WHERE ID = ?
UPDATE MY_TABLE SET ID=?, NAME=?, DESC=?, STATUS=? WHERE ID = ?
What can I do to skip the auto-updation?
Edit:
Here are the routines we use for executing the queries.
The following routine returns a named native query sql.
public String getNamedNativeQuerySql(EntityManagerFactory emf, String qryName) {
MetamodelImpl metamodel = (MetamodelImpl) emf.getMetamodel();
QueryMetaData queryMetaData =
metamodel.getConfiguration().getMetaDataRepositoryInstance().getQueryMetaData(null, qryName, null, true);
String queryString = queryMetaData.getQueryString();
return queryString;
}
The code for retrieval:
Query query = entityManager.createNamedQuery("retrieveQry");
query.setParameter(1, id);
Object[] result = (Object[]) query.getSingleResult();
MyEntity entity = (MyEntity) result[0];
String tempFileName = (String) result[1];
The code for update that follows retrieval:
Query qry = entityManager.createNamedQuery("updateQry");
qry.setParameter(1, status);
qry.setParameter(2, entity.getId() );
qry.executeUpdate()
Edit:
I see the problem even without the update statement. OpenJPA is
executing an additional update query even if I do a simple find.
The problem was with runtime enhancement. OpenJPA was unable to do a proper detection of dirty state with runtime-enhanced entities.
It got resolved by doing a build time enhancement.