DistinctResultList in OpenJPA - openjpa

I am attempting to get a distinct list of id's back that would be longs... But I am getting back this DistinctResultList... How can one handle this so that they get back results they are expecting... Here is what I am doing...
#NamedQuery(name="getProvidersByResourceIds", query = "SELECT DISTINCT p.resourceId FROM Provider p WHERE p.resourceId `in :resourceIds")`
Out of that I try and get the resourceIds' by doing this...
List<Long> provIDs = (List<Long>) emf.createNamedQuery("getProvidersByResourceIds").setParameter("resourceIds", values).getResultList();
But like I said, I keep getting back a DistinctResultList... Looking through the debugger, I can see the values I am getting back. How can I translate this into something usefull?
javax.ejb.EJBException: See nested exception; nested exception is: java.lang.IllegalArgumentException: Parameter "Parameter<long>('resourceIds')" declared in "SELECT p FROM Provider p WHERE p.resourceId = :resourceIds" is set to value of "org.apache.openjpa.kernel.DistinctResultList#35fb35fb" of type "org.apache.openjpa.kernel.DistinctResultList", but this parameter is bound to a field of type "long".
java.lang.IllegalArgumentException: Parameter "Parameter<long>('resourceIds')" declared in "SELECT p FROM Provider p WHERE p.resourceId = :resourceIds" is set to value of "org.apache.openjpa.kernel.DistinctResultList#35fb35fb" of type "org.apache.openjpa.kernel.DistinctResultList", but this parameter is bound to a field of type "long"

Change your query to :
#NamedQuery(name="getProvidersByResourceIds",
query = "SELECT DISTINCT p.resourceId FROM Provider p WHERE p.resourceId in (:resourceIds)");

Related

Why does this JPQL fail with message "You have attempted to set a parameter at position x which does not exist in this query string"?

I have the following named query:
#NamedQueries({
#NamedQuery(name ="Movies.findByTitle", query = "SELECT m FROM Movies m WHERE m.title = :title")
})
When I try to execute it as follows:
public List<T> findByTitle(String title) {
return getEntityManager().
createNamedQuery("Movies.findByTitle").
setParameter("findByTitle", title).
getResultList();
}
Then I get the following exception:
You have attempted to set a parameter value using a name of findByTitle that does not exist in the query string SELECT m FROM Movies m WHERE m.title = :title.
How is this caused and how can I solve it?
You need to set a correct parameter name.
You can see in your query the parameter is named "title" not "findByTitle".
setParameter("findByTitle", title) change this to setParameter("title", title)

DQL to query type fragments in documentum

I have an object in docmentum, this object has attributes and type fragments, I want to query by a type fragment attribute value. My object name is CIC and it has a type fragment called Emision which has an attrribute called receptor, so when I do :
select * from dfl_cic c where c.emision.receptor = "XXXXX"
I get an error, so how I can query by this value ??

In clause parameters with SPEL

I'm trying to pass multiple In Clause parameters using Spring-El, my query is:
#Query(nativeQuery = true, value = "select a.* from ACC a where (a.iban, a.currency) in (:#{#ibanAccounts.accountNumber} , :#{#ibanAccounts.currency})")
List<ACC> getAccount(#Param("ibanAccounts") List<AccountDetails> ibanAccounts);
But I'm getting following error:
EL1008E:(pos 14): Property or field 'accountNumber' cannot be found on object of type 'java.util.ArrayList$SubList' - maybe not public?
org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 14): Property or field 'accountNumber' cannot be found on object of type 'java.util.ArrayList$SubList' - maybe not public?
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:224)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:46)
at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:374)
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:88)
at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:131)
In AccountDetails the field is present with public getter.
Can anyone please guide me what's wrong?
I believe what you are looking for is collection projection.
So you need to use something like this
#Query(nativeQuery = true, value = "select a.* from ACC a where (a.iban, a.currency) in :#{#ibanAccounts.![accountNumber]} , :#{#ibanAccounts.currency})")
List<ACC> getAccount(#Param("ibanAccounts") List<AccountDetails> ibanAccounts);

JPQL: Parameter with that position [1] did not exist

I am working with JPA and JPQL. Using a JPQL I would like to fetch join a collection that is an attribute of a "main" entity rent. Here is my source code:
public Rent getRentWithAllDetails(Rent rent) {
Query queryString = em.createQuery(" select r from Rent r JOIN FETCH r.rentables where r.id = :rid").setParameter(1, rent.getId());
List <Rent> resultList = queryString.getResultList();
return resultList.get(0);
}
and this is the exception I recieve:
Caused by: java.lang.IllegalArgumentException: Parameter with that position [1] did not exist
at org.hibernate.jpa.spi.BaseQueryImpl.findParameterRegistration(BaseQueryImpl.java:518) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:674) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:198) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:49) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
Could someone help me, please?
You are using a named parameter, so you should bind a parameter by that name while creating your query:
String sql = "select r from Rent r JOIN FETCH r.rentables where r.id = :rid";
Query queryString = em.createQuery(sql)
.setParameter("rid", rent.getId());
List<Rent> resultList = queryString.getResultList();
Please change your code to:
public Rent getRentWithAllDetails(Rent rent) {
Query queryString = em.createQuery(" select r from Rent r JOIN FETCH r.rentables where r.id = :rid").setParameter("rid", rent.getId());
List <Rent> resultList = queryString.getResultList();
return resultList.get(0);
}
if rid is repeated multiple times, you can use 0,1..etc accordingly, otherwise use the parameter name itself.

JPQL query with WHERE on nested fields

I have a java entity class UserBean with a list of events:
#OneToMany
private List<EventBean> events;
EventBean has Date variable:
#Temporal(javax.persistence.TemporalType.TIMESTAMP)
private Date eventDate;
Now in UserBean I want to create a NamedQuery that returns all dates that fall within a specific range:
#NamedQuery(name="User.findEventsWithinDates",
query="SELECT u.events FROM UserBean u WHERE u.name = :name AND u.events.eventDate > :startDate AND u.events.eventDate < :endDate")
The above query does not compile though. I get this error:
The state field path 'u.events.eventDate' cannot be resolved to a valid type.
By the way, I use EclipseLink version 2.5.0.v20130507-3faac2b.
What can I do to make this query work? Thanks.
Path u.events.eventDate is an illegal construct in JPQL, because it is not allowed to navigate via a collection valued path expression. In this case u.events is a collection valued path expression. In JPA 2.0 specification this is told with following words:
It is syntactically illegal to compose a path expression from a path
expression that evaluates to a collection. For example, if o
designates Order, the path expression o.lineItems.product is illegal
since navigation to lineItems results in a collection. This case
should produce an error when the query string is verified. To handle
such a navigation, an identification variable must be declared in the
FROM clause to range over the elements of the lineItems collection.
This problem can be solved by using JOIN:
SELECT distinct(u)
FROM UserBean u JOIN u.events e
WHERE u.name = :someName
AND e.eventDate > :startDate
AND e.eventDate < :endDate