Kundera Mongodb pass array object in where clause - mongodb

I want to pass multiple values in where clause,for that i am passing the array but the result returning empty.
List<String> stringlist=new ArrayList<String>();
stringlist.add("54aca811edc5a179efd28e55");
stringlist.add("54aca79aedc5158d25a93f42");
Query q = em
.createQuery("Select s from StoreProductMaster s where s.store_id = :ids");
q.setParameter("ids",stringlist);
List<StoreProductMaster> tweets = q.getResultList();
Help me

#Sreekant
As you need to fetch results for multiple ids from a collection , don't you think that your query should contain "in" clause instead of "=" ?
Query q = em
.createQuery("Select s from StoreProductMaster s where s.store_id in :ids");
q.setParameter("ids",stringlist);
List tweets = q.getResultList();
Chhavi

Related

How to write this query in JPA

How to write this query in JPA?
select sol.ID_UNICA, sol.version
from uexdfr01.SOLUTION sol
join
(select solution4_.ID_UNICA, max(solution4_.version) as maxVersion
from uexdfr01.ORDER order3_
inner join uexdfr01.SOLUTION solution4_ on order3_.ID_SOLUTION=solution4_.ID_SOLUTION
where solution4_.ID_UNICA in (130,139,143,129,126,128,141,121,124,131)group by solution4_.ID_UNICA) as groupedtt
on sol.ID_UNICA = groupedtt.ID_UNICA
AND sol.version = groupedtt.maxVersion*
If you are not sure about db relationships use createNativeQuery and pass your query inside parenthesis.
Query selectRecords = getEntityManager().createNativeQuery(sqlQuery.toString());
Returned values are type of Objects so make sure to assign them in List as following way..
List<Object[]> data= selectRecords.getResultList();

How to fetch records using JPQL with case insensitive match

I am trying to fetch the records of employees using JPQL but I don't know name with exact case.
select e from Employee e where e.name = 'rahul'
what changes I need to make in above JPQL to get the list of employee whose name is Rahul, rahul or RAHUL etc.
I got a way to fetch the records, though not sure if this is efficient one or not.
TypedQuery<Employee> query = em.createQuery("select e from Employee e where lower(e.name) = :name", Employee.class);
query.setParameter("name", name.toLowerCase());
query.getResultList();

Criteriabuilder like, how to do it for Long?

i try use "like" method from Criteriabuilder for get all record based on pattern " 10% ".
I want get record where ID is - 101, 10002, 1003,1000 etc...
I've use this code:
Predicate p = cb.like(r.<String>get("ID").as(String.class), "10%")
but i got Exception where i see what postgres can't execute query like this:
SELECT ID, NAME, SOMETHING FROM TABLE WHERE ID LIKE 10%
That is JPA (Glassfish 4.x) generate wrond query.
Right query must like that :
SELECT ID, NAME, SOMETHING FROM TABLE WHERE CAST (ID as TEXT) LIKE '10%'
How to build query via Criteria API that i got a right query for postgres ?
Updated:
I try write a CAST function :
Expression<String> postgresqlCastFunction = cb.function("CAST", String.class, r.<String>get("ID").as(String.class));
Predicate p = cb.like(postgresqlCastFunction, "10%");
but got a query like this :
FROM TABLE WHERE (CAST(ID) LIKE ?)
, so, how to add need expression in function for this right result -
FROM TABLE WHERE (CAST(ID as TEXT) LIKE ?) ..
An example implementation using PostgreSQL native TO_CHAR function may look as follows:
JPQL
SELECT r FROM Records r
WHERE FUNCTION('TO_CHAR', r.ID, 'FM9999999999') LIKE :pattern
Criteria API
Path<String> id = r.get("ID");
Expression<String> format = cb.literal("FM9999999999");
Expression<String> function= cb.function("TO_CHAR", String.class, id, format);
ParameterExpression<String> pattern = cb.parameter(String.class, "pattern");
Predicate like = cb.like(function, pattern);
cq.where(like);
also you can build the query as an one-liner:
cq.where(cb.like(cb.function("TO_CHAR", String.class, r.get("ID"), cb.literal("FM9999999999")), cb.parameter(String.class, "pattern")));
Execute the above query:
Query q = em.createQuery(cq).setParameter("pattern", "10%");

Join Two table in Criteria Query

I have three tables one is ItemCategory,ItemMaster and Price. I am referring itemaCategoryId in ItemMaster table and like that referring itemmasterid in price. Now i have to display contents of price order by itemcategory id. This is my criteria query.
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Price> cq = cb.createQuery(Price.class);
Root<Price> root = cq.from(Price.class);
Root<ItemMaster> itemMasterRoot = cq.from(ItemMaster.class);
Root<ItemCategory> itemCategoryRoot = cq.from(ItemCategory.class);
Join<ItemMaster, ItemCategory> s=itemMasterRoot.join(ItemMaster_.category);
Join<Price,ItemMaster> y=root.join(Price_.itemMaster);
Path<Long> itemMasterId=root.get(Price_.itemMasterId);
cq.select(root).where(cb.equal(root.get(Price_.priceMasterId), priceMasterId))
.orderBy(cb.asc(itemMasterId));
TypedQuery<Price> q = entityManager.createQuery(cq);
Above my criteria Query
If you use multiple from statements, you get the cartesian product of all entities. If you want to preserve the relationships, use join instead:
Root<Price> price = cq.from(Price.class);
Join<Price,ItemMaster> itemMaster = price.join(Price_.itemMaster);
Join<ItemMaster, ItemCategory> itemCategory = itemMaster.join(ItemMaster_.category);
However it looks like at least the second join may be useless, because you are able to access the category property directly using the getter, isn't it?:
Price aPriceResult;
ItemCategory categoryResult = aPriceResult.getItemMaster().getCategory();

Wildcard not working? - JPQL in JPA

I've got a class MovieElement, a field url which is the id, and an other field name.
Just for testing the values of name and url are the same.
An object is persisted with name & url = "www.test.com"
But I cannot get the wildcard working, I tried a query like:
query = em.createQuery("SELECT m FROM MovieElement m WHERE m.name LIKE :keyword");
query.setParameter("keyword", "%test%");
query.getResultList();
This gives an empty result.
But the following works:
query = em.createQuery("SELECT m FROM MovieElement m WHERE m.name LIKE :keyword");
query.setParameter("keyword", "www.test.com");
query.getResultList();
Evenso all the next ones results in a null:
MovieElement el = em.find(MovieElement.class, "%test%");
query = em.createQuery("SELECT m FROM MovieElement m WHERE m.name LIKE :keyword");
query.setParameter("keyword", "www.test.co_");
query.getResultList();
query = em.createQuery("SELECT m FROM MovieElement m WHERE m.name LIKE :keyword");
query.setParameter("keyword", "%");
query.getResultList();
Due to the last query it seems the code doesn't take the wildcard into account.
Further I'm using DataNucleus and MongoDb.
Anyone an idea? Thx!
Found the solution: Seems like Datanucleus for mongodb recognizes ".*" as the wildcard instead of "%"