How can I use find_in_set in jpa?
I need to achieve like this
SQL - select * from teacher where find_in_set("5", deptIds) and id = 101
where deptIds have comma separated ids (I know it's bad idea but legacy.)
To do so I had been tried using Criteria but not found any Restrictions that can fulfill find_in_set.
Note - need possible solution with Criteria and Restrictions
criteriaBuilder.function("find_in_set", Boolean.class,
criteriaBuilder.literal(s),
root.get("field"))
Here is an example:
Here is java code snippet.
list.add(cb.greaterThan(cb.function("FIND_IN_SET", Integer.class,
cb.literal(val.toString()), root.get(attributeName)), 0));
select t from Teacher t where find_in_set("5", t.deptIds) = 0 and t.id = 101
Related
I am new to Java and Querydsl. I have searched so much to get how to write the below query in query dsl. end up with nothing.Can any one please help.
SELECT * FROM `library_user_product` GROUP by user_id HAVING max(product_id)
Thanks in advance
Use this link it has examples for the group by, having and other function in respect to JPA
sample:
SELECT c.currency, SUM(c.population) FROM Country c
WHERE 'Europe' MEMBER OF c.continents
GROUP BY c.currency
HAVING COUNT(c) > 1
I am have a problem where i need to join two tables using the LEAST and GREATEST functions, but using JPA CriteriaQuery. Here is the SQL that i am trying to duplicate...
select * from TABLE_A a
inner join TABLE_X x on
(
a.COL_1 = least(x.COL_Y, x.COL_Z)
and
a.COL_2 = greatest(x.COL_Y, x.COL_Z)
);
I have looked at CriteriaBuilder.least(..) and greatest(..), but am having a difficult time trying to understand how to create the Expression<T> to pass to either function.
The simplest way to compare two columns and get the least/greatest value is to use the CASE statement.
In JPQL, the query would look like
select a from EntityA a join a.entityXList x
where a.numValueA=CASE WHEN x.numValueY <= x.numValueZ THEN x.numValueY ELSE x.numValueZ END
and a.numValueB=CASE WHEN x.numValueY >= x.numValueZ THEN x.numValueY ELSE x.numValueZ END
You can code the equivalent using CriteriaBuilder.selectCase() but I've never been a big fan of CriteriaBuilder. If requirements forces you to use CriteriaBuilder then please let me know and I can try to code the equivalent.
CriteriaBuilder least/greatest is meant to get the min/max value of all the entries in one column. Let's say you want to get the Entity that had the alphabetically greatest String name. The code would look like
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery query = cb.createQuery(EntityX.class);
Root<EntityX> root = query.from(EntityX.class);
Subquery<String> maxSubQuery = query.subquery(String.class);
Root<EntityX> fromEntityX = maxSubQuery.from(EntityX.class);
maxSubQuery.select(cb.greatest(fromEntityX.get(EntityX_.nameX)));
query.where(cb.equal(root.get(EntityX_.nameX), maxSubQuery));
I created a sample Spring Data JPA app that demonstrates these JPA examples at
https://github.com/juttayaya/stackoverflow/tree/master/JpaQueryTest
It turns out that CriteriaBuilder does support calling LEAST and GREATEST as non-aggregate functions, and can be accessed by using the CriteriaBuilder.function(..), as shown here:
Predicate greatestPred = cb.equal(pathA.get(TableA_.col2),
cb.function("greatest", String.class,
pathX.get(TableX_.colY), pathX.get(TableX_.colZ)));
I'd like to use criteria and create something like this but with hibernate criterias:
WHERE concat(X, Y) like '%Some Value%'
I don't want to do it using query concatenation, but using Restrictions because I am making a dynamic library that gets the left and right side of the like operator? Am I clear? Please if not ask me to give more clarification.
Thank you!
Hope it may works.
Criteria crit = session.createCriteria(Table.class);
crit.add(Restrictions.ilike("param1 || param2", "%" + dniWithLetter + "%"));
If the above code not working, please go through the following link.
Can we concatenate two properties in Hibernate HQL query?
I think that the best solution for your problem is the following
...
Object[] params = {objX, objY, someValue};
Type[] typeOfParams = {Hibernate.STRING, Hibernate.STRING, Hibernate.STRING};
Criteria crit = session.createCriteria(Table.class);
crit.add(Restrictions.sqlRestriction("concat(?, ?) like '%?%'", params, typeOfParams));
...
When you use Restrictions.sqlRestriction, you can build a complex query with multiple parameters, you just have to set the parameters and type of each parameter, this is done with params array and typeOfparams array.
I hope this information helps you.
Good luck.
Is there a way to have named parameters in Java MySQL query?
Like this:
SELECT * FROM table WHERE col1 = :val1 AND col2 = :val2
instead of this:
SELECT * FROM table WHERE col1 = ? AND col2 = ?
UPDATE: Im' using java.sql.*, however would be interested in alternatives capable of this.
Maybe Hibernate is good choice for you. It provided the query style as your description, and it's so powerful and convenient to do persistence work that you'll feel cool.
e.g
Query query = sesion.createQuery("from Student s where s.age = :age");
query.setProperties(student);
see the doc:http://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/Query.html#setParameter(java.lang.String, java.lang.Object)
The excellent JDBI library lets you do this and much more.
Yes, there are alternatives. By example, with javax.persistence.* you can achieve that quite easily.
http://docs.oracle.com/javaee/6/tutorial/doc/bnbrg.html
An entity manager will allow you to create dynamic (parametrized) queries with the methods EntityManager.createQuery, and EntityManager.createNamedQuery.
I have some query like
SELECT * FROM JobTable
WHERE isnull(retryCount,0)<3
AND updatedOn < dateadd(MI,-5,getdate())
How to convert it to Criteria api calls? Point to use Criteria is to allow refactoring if fields names will be changed.
For simple things this is looks like
criteria.add(Restrictions.lt(JobTable.RETRYCOUNT_FULL, 3));
But what about my case?
criteria.add(Restrictions.lt(JobTable.UPDATEON_FULL, <???>);
criteria.add(Restrictions.lt( <someexpression(JobTable.RETRYCOUNT_FULL)> , 3));
I'm not sure of the correct name but hibernate has some standard functions which are mapped to the dialects functions. They can be used like this
Projections.sqlFunction("dateadd", Projections.property("property"), Hibernate.dateTime);
Hibernate Criteria API has Restrictions.sqlRestriction functions. In these functions you write your SQL the way you like and Hibernate embeds it in to your SQL.
criteria.add(Restrictions.sqlRestriction("isnull({alias}" + JobTable.RETRYCOUNT_FULL + ", 0) < ?", 3, new IntegerType());