Search nested object with spring data, Illegal attempt to dereference path source - spring-data-jpa

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);

Related

Spring Data JPA + JpaSpecificationExecutor + NamedEntityGraph

I have two Entities. Parent and Child.
#Entity
public class Parent {
#Id
#Column(name = "PARENT_ID")
private BigInteger id;
#Column(name = "FIRST_NAME")
private String firstName;
#Column(name = "LAST_NAME")
private String lastName;
}
#Entity
#NamedEntityGraph(name = "Child.parentObj", attributeNodes = #NamedAttributeNodes("parentObj"))
public class Child{
//blah blah blah
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name="PARENT_ID")
Parent parentObj;
#OneToMany(fetch = FetchType.LAZY)
#JoinColumn(name="CHILD_ID")
Set<Address> address
//blah blah blah
}
ChildRepository.java
public interface ChildRepository extends JpaRepository<T, ID>, JpaSpecificationExecutor<T>{
#EntityGraph(value="Child.parentObj")
public List<Child> findAll(Specification<Child> spec);
}
I am trying to find child entities by criteria and it should always have parent.
I am getting exception that it is trying to find parentObj in Address table.
Caused by: org.hibernate.QueryException: could not resolve property: parentObj of: com.xxx.Address [..]
I found this link and tried solution given by Joep but same error.
Spring Data JPA + JpaSpecificationExecutor + EntityGraph
what am I missing. I am not able to understand why/how i limit to look for parentObj in just Child Object as Address has no reference to Parent.
I am doing search by Address.street.
I have tried ad-hoc entity graph too. exception:
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.QueryException: could not resolve property: parentObj of: com.xxx.Address
Caused by: org.hibernate.QueryException: could not resolve property: parentObj of: com.xxx.Address
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:77)
at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1978)
at org.hibernate.hql.internal.ast.tree.FromElementType.getPropertyType(FromElementType.java:367)
at org.hibernate.hql.internal.ast.tree.FromElement.getPropertyType(FromElement.java:500)
at org.hibernate.engine.query.spi.EntityGraphQueryHint.getFromElements(EntityGraphQueryHint.java:95)
at org.hibernate.engine.query.spi.EntityGraphQueryHint.toFromElements(EntityGraphQueryHint.java:68)
at org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:676)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:665)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.collectionFunctionOrSubselect(HqlSqlBaseWalker.java:4905)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:4606)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2104)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2029)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2029)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:796)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:597)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
... 60 more
I wanted to find Child entries by criteria search and get ParentObj for each child. I wanted to do join and not individual select for Parent. I was trying to solve that by using EntityGraph but it didn't work. As #EKlavya pointed out Specification and EntityGraph don't work togather.
The way I solved was:
root.fetch("parentObj", JOIN.LEFT);
in my toPredicate method. this will get Child entity with Parent in 1 query.
You are using Child.parentObj as Entity graph name but you named the entity graph as Child.parent. Use
#EntityGraph(value="Child.parent")
Without #NamedEntityGraph
we can define an ad-hoc entity graph, without #NamedEntityGraph just use
#EntityGraph(attributePaths = {"parentObj"})
Update:
Entity graph and specification both are not working together.
There is a way to solve this. Don't do eager for parent fetch child only first then make a list of child ids from child's and get parents using in clause query using child ids. Total only 2 queries needed. If you want to solve this using 1 query use DSL to do raw query.

How to read a collection property with JPA?

I have two classes, Account and Admin, with many to many mapping.
The Admin class has a collection of Account class and vise versa.
I want to write a query, that given the account id, will return all the account admins.
Here is the relevant fields of the Account class:
#Entity
public class Account {
#Id
public Long id;
#ManyToMany(mappedBy = "account", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
public List<Admin> users = new ArrayList<>();
}
I have tried a regular query for Admin.class with multiselect as each account has a collection of admins, but trying to get a TypedQuery<Admin> out of my CriteriaQuery<Admin> I got an IllegalArgumentException with the message "org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [models.Admin]. Expected arguments are: java.util.Collection [select new models.Admin(generatedAlias0.users) from models.Account as generatedAlias0 where generatedAlias0.id=1L]" (1L here probably since I called this function with 1 as accountId), caused by QuerySyntaxException with the message "Unable to locate appropriate constructor on class [models.Admin]. Expected arguments are: java.util.Collection".
Code:
private static List<Admin> readAccountAdmins(Long accountId) {
CriteriaBuilder cb = JPA.em().getCriteriaBuilder();
CriteriaQuery<Admin> cq = cb.createQuery(Admin.class);
Root<Account> root = cq.from(Account.class);
Predicate idPredicate = cb.equal(root.get(Account_.id), accountId);
cq.multiselect(root.get(Account_.users)).where(idPredicate);
TypedQuery<Admin> typedQuery = JPA.em().createQuery(cq); // exception thrown here
return typedQuery.getResultList();
}
After that I tried running a TypedQuery<List<Admin>>, as I am trying to read a list. This is the first iteration of trying a query of list:
private static List<Admin> readAccountAdmins(Long accountId) {
CriteriaBuilder cb = JPA.em().getCriteriaBuilder();
CriteriaQuery<List<Admin>> cq = cb.createQuery((Class<List<Admin>>)(Class<?>)(Collection.class));
Root<Account> root = cq.from(Account.class);
Predicate idPredicate = cb.equal(root.get(Account_.id), accountId);
cq.select(root.get(Account_.users)).where(idPredicate);
TypedQuery<List<Admin>> typedQuery = JPA.em().createQuery(cq);
return typedQuery.getSingleResult(); // exception thrown here
}
I used getSingleResult as getResultList caused a compilation error, saying the actual return value is List<List<Admin>>> and doesn't match the signature.
This method threw a different exception, a NonUniqueResultException with the message: "result returns more than one elements".
While debugging, I tried to evaluate the expression typedQuery.getResultList() and saw that it actually returns List<Admin> and not List<List<Admin>>, so I got to my final iteration of this function:
private static List<Admin> readAccountAdmins(Long accountId) {
CriteriaBuilder cb = JPA.em().getCriteriaBuilder();
CriteriaQuery<List<Admin>> cq = cb.createQuery((Class<List<Admin>>)(Class<?>)(Collection.class));
Root<Account> root = cq.from(Account.class);
Predicate idPredicate = cb.equal(root.get(Account_.id), accountId);
cq.select(root.get(Account_.users)).where(idPredicate);
TypedQuery<List<Admin>> typedQuery = JPA.em().createQuery(cq);
return (List) typedQuery.getResultList();
}
Now, this function works, but my question is why?
Why did the compiler decide that getResultList returns a different value than the actual return value?
Maybe it makes sense when you take a closer look at your database. A TypeQuery returns entities, so basically rows from tables. List<Admin> is a collection of Entities, so eventhough your Account has a List<Admin> as a field, the Query will still return List<Admin> entities, not List<List<Admin>> as List<Admin> is not an entity.
I hope that makes sense.

Saving value in Hibernate to another table than entity table

I have two tables bo_operator and hist_bo_operator_password. In bo_operator the id column is foreign key to hist_bo_operator_password and I can have many the same operator_id in hist_bo_operator_password and only one id in bo_operator.
My entity:
#Entity
#Table(name="bo_operator")
public class Operator implements Serializable
and that's how I am getting values from hist_bo_operator_password:
#ElementCollection
#CollectionTable(name="hist_bo_operator_password", joinColumns=#JoinColumn(name="id_operator"))
#Column(name="password")
public List<String> oldPasswords = new ArrayList<String>();
but when I'm trying to get only one value by:
#ElementCollection
#CollectionTable(name="hist_bo_operator_password", joinColumns=#JoinColumn(name="id_operator"))
#Column(name="password")
public String oldPassword;
I'm getting error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Illegal attempt to map a non collection as a #OneToMany, #ManyToMany or #CollectionOfElements: local.vlex.operator.model.Operator.oldPassword
and all I want to do is makeing an insert into hist_bo_operator_password by
operator.setOldPassword(oldPassword);. I think the problem is that it doesn't know which password take if there is many values for the same id.
How to achive it?
#Edit
I also tried:
#Table(name="bo_operator")
#SecondaryTable(name = "hist_bo_operator_password",pkJoinColumns=#PrimaryKeyJoinColumn(name="id_operator", referencedColumnName="id"))
I even found ORDER BY so:
#Column(name="password", table="hist_bo_operator_password")
#OrderBy("data_ins")
public String oldPassword;
but seems like there is no #Limit or something like this in JPA and I still have many values to the same id which cause error:
org.hibernate.HibernateException: Duplicate identifier in table for: [local.vlex.operator.model.Operator#1]
As you described at the first sentence you have one-to-many relationship
#ElementCollection
#CollectionTable(name="hist_bo_operator_password", joinColumns=#JoinColumn(name="id_operator"))
#Column(name="password")
#OrderBy("data_ins")
public List<String> oldPasswords = new ArrayList<String>();
Then add necessary getter
Optional<String> getPassword() {
return oldPasswords.size() > 0
? Optional.of(oldPasswords.get(0))
: Optional.empty();
}
And setter
void setPassword(String password) { // or maybe addPassword?
oldPasswords.add(password);
}
Why don't you create entity of hist_bo_operator_password?
Then you could have List of that entities (instead of just String List) and just add another object to List and save entity Operator.

QueryInvalidParametersException: Query has parameter "surveyId" supplied at execution yet the query doesnt make use of this parameter

#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

Querying a collection of embeddable elements using eclipselink

I have the following JPA (2.0.2) entities:
Employee
#Entity
#Table(name = "T_EMPLOYEE")
public class Employee {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#ElementCollection
#CollectionTable(name = "T_COMPETENCE")
private Set<Competence> competences;
// Getter and setters
}
and Competence
#Embeddable
public class Competence {
#JoinColumn(nullable = false)
#ManyToOne
private Skill skill;
// Getter and setters
}
(The skill entity shouldn't be important and is therefore omitted, so are various additional attributes.)
I'm using EclipseLink (2.2.0) to query my entities with a DAO. Now I'd like to use the following query:
public List<Employee> findBySkill(Skill skill) {
TypedQuery<Employee> query = getCurrentEntityManager().createQuery(
"SELECT e FROM Employee e JOIN e.competences c WHERE c.skill = :skill",
Employee.class);
query.setParameter("skill", skill);
return query.getResultList();
}
But it keeps throwing the following exception:
Caused by: Exception [EclipseLink-8030] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Error compiling the query [SELECT e FROM Employee e JOIN e.competences c WHERE c.skill = :skill], line 1, column 54: unknown state or association field [skill] of class [com.kaio.model.Competence].
at org.eclipse.persistence.exceptions.JPQLException.unknownAttribute(JPQLException.java:457)
at org.eclipse.persistence.internal.jpa.parsing.DotNode.validate(DotNode.java:88)
at org.eclipse.persistence.internal.jpa.parsing.Node.validate(Node.java:91)
at org.eclipse.persistence.internal.jpa.parsing.BinaryOperatorNode.validate(BinaryOperatorNode.java:34)
at org.eclipse.persistence.internal.jpa.parsing.EqualsNode.validate(EqualsNode.java:41)
at org.eclipse.persistence.internal.jpa.parsing.WhereNode.validate(WhereNode.java:34)
at org.eclipse.persistence.internal.jpa.parsing.ParseTree.validate(ParseTree.java:207)
at org.eclipse.persistence.internal.jpa.parsing.ParseTree.validate(ParseTree.java:183)
at org.eclipse.persistence.internal.jpa.parsing.ParseTree.validate(ParseTree.java:173)
at org.eclipse.persistence.internal.jpa.parsing.JPQLParseTree.populateReadQueryInternal(JPQLParseTree.java:110)
at org.eclipse.persistence.internal.jpa.parsing.JPQLParseTree.populateQuery(JPQLParseTree.java:84)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:216)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:187)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:139)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:123)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1376)
... 48 more
The message is pretty clear: It can't find the attribute Skill on my class Competence. But in my opinion there is no reason on that. Or do I have a wrong approach on my problem? How should I query in a List of embeddable objects?
Any help is appreciated.
Okay, this seems to be a bug and it has been solved in a later version of eclipse link. I updated the project to EclipseLink 2.4 and the problem disappeared.
Your Competence table will have a Skill.id field, so try:
"SELECT e FROM Employee e JOIN e.competences c WHERE c.skill.id = :skillId"
query.setParameter("skillId", skill.getId());