I want to convert my sql query from SQL to Criterias (i dont want to use JPQL), i have this sql query:
SELECT * FROM (
SELECT CONCAT_WS(' ',p.first_name, p.middle_name, p.last_name) AS fullname FROM persons p) AS tmp
WHERE fullname LIKE '%cetina avila%' ORDER BY fullname;
How is the best way to do this?, Im trying to search full names from my talbe persons.
Thanks
One way to do this is to use an #Formula mapping on your Person class like this
#Formula("CONCAT(first_name, ' ', middle_name, ' ', last_name)")
private String fullname;
Then in your Criteria you can search it like any normal field.
See Calculated property with JPA / Hibernate for more on calculated fields.
Related
Table name: Employee
I have an array column name: ["a","b","c"]
and a value name -> "a" which i want to check if it is present in array.
Through sql query i am able to do using ANY clause:
SELECT *
FROM EMPLOYEE WHERE 'a' = ANY(name)
But when i try to do same through criteria query I run into issues as ANY() uses subquery as param.
Can someone help how to achieve same using criteria api without using subquery?
I have tried isMember,Any() In() using criteria query but none of that works.
I am using postgres DB and i have table with two column name and sal .
name Sal
Raunak 10000
Raunak 5000
Rahul 500
Raunak 300
And i want
Raunak 10000,5000,300
Rahul 500
i am using JPA is there any way to get in JPA data
You can use string_agg function to build a comma separated list of values:
select name, string_agg(sal::text, ',')
from t
group by name
You might want to consider json_agg instead of csv if your application can consume json data.
If you want to preserve the data type of the sal column, you can use array_agg() that returns an array of values. Not sure if JPA will let you access that properly though
select name, array_agg(sal) as sals
from the_table
group by name;
If I understand your question correctly, you want to get the result via below SQL statement:
SELECT
name,
string_agg (sal::varchar(22), ', ') as sals
FROM
test
GROUP BY
name;
Since it's postgresql related SQL, we can't or hard to express it via common object query. You can construct the above SQL via native query mode in your JPA code.
#Query(value = "<the above query>", nativeQuery = true)
List<Object[]> query();
I am trying to search a join table of millions of records using JPA native query and PostgreSQL but the result is too slow. I want to make it faster using index search on postgres or any better option that can be suggested. I looked up several articles online like this:
https://hashrocket.com/blog/posts/exploring-postgres-gin-index
but am not quite sure how to implement it as am not getting the improved speed.
Here is what I had in my JPA repository:
#Query(value = "SELECT DISTINCT C.APPROVED_NAME AS companyName, C.CITY AS city, C.STATE AS state,
FROM TABLE_NAME_1 C LEFT JOIN TABLE_NAME_2 AS A ON A.COMPANY_FK = C.ID
WHERE CONCAT(UPPER(A.FIRSTNAME), ' ', UPPER(A.SURNAME))
LIKE UPPER(concat(:searchTerm,'%')) ", nativeQuery = true)
List<table_1_Interface> findByName(#Param("searchTerm") String searchTerm);
Here is what I tried to do using index search (I am new to using index search):
#Query(value = "CREATE INDEX company_search_idx ON TABLE_NAME_2(firstname, surname );
SELECT DISTINCT C.APPROVED_NAME AS companyName, C.CITY AS city, C.STATE AS state,
FROM TABLE_NAME_1 C LEFT JOIN TABLE_NAME_2 AS A ON A.COMPANY_FK = C.ID
WHERE CONCAT(UPPER(A.FIRSTNAME), ' ', UPPER(A.SURNAME))
LIKE UPPER(concat(:searchTerm,'%')) ", nativeQuery = true)
List<table_1_Interface> findByName(#Param("searchTerm") String searchTerm);
Please help me with your suggestions, ideas and solutions.
If you search on CONCAT(UPPER(A.FIRSTNAME), ' ', UPPER(A.SURNAME)) then that is what the index needs to be built on (but without the table aliases). But for pg_trgm it supports ILIKE directly, so you could just use that instead of manually fiddling with case.
Since you only add a % to the end of the search term, you could instead use a btree index with text_pattern_ops. But then you would need the UPPER with LIKE.
I have the following query which runs perfectly in mysql.
SELECT * FROM Orders as o, Products as p where o.productinfo RLIKE p.code;
Here I am joining two tables Orders and Products with RLIKE.
I am trying to implement the same in Hibernate.
Query query = session.createQuery("FROM Orders as o, Products as p where o.productinfo RLIKE p.code");
List<Object[]> results = query.getResultList();
When I used RLIKE, the following error is thrown in run time.
{"errormessage":"org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: RLIKE
I tried to implement the same with LIKE query and matched it with '%p.code%'.
Query query = session.createQuery("FROM Orders as o, Products as p where o.productinfo LIKE '%p.code%'");
But it matches with the string "p.code" rather then the value.
What is the equivalent of RLIKE in HQL?
Is there a different way to join two tables with LIKE in HQL?
Thanks.
Answer by #YCF_L:
For any one trying to join two tables with like operator in Hibernate (mysql) can do it the following way.
SELECT * FROM Orders as o, Products as p where o.productinfo LIKE CONCAT('%',p.code,'%');
What's the equivalent of mysql RLIKE operator in Hibernate Query?
RLIKE is the synonym for REGEXP so you can implement it in hibernate using REGEXP_LIKE, you can take a look about this here : How to search in multiple columns using one like operator in HQL (hibernate sql)
I tried to implement the same with LIKE query and matched it with
'%p.code%'.
..., Products as p where o.productinfo LIKE '%p.code%'");
But it matches with the string "p.code" rather then the value.
This is true, because you don't pass the correct value of p.code, you pass it like a String, instead you have two ways :
Query query = session.createQuery("....Products as p where o.productinfo LIKE '%:code%'");
//------------------------------------------------------------------------------^^^^^^^
query.setParameter("code", p.code);
Or you can concatenate your code with your Query, but the first solution is better.
Query query = session.createQuery(".... where o.productinfo LIKE '%" + p.code + "%'");
EDIT
You can use like with CONCAT without specify the '' like this :
SELECT * FROM Orders as o, Products as p where o.productinfo LIKE CONCAT('%',p.code,'%');
You can check regular expression way something like this way: Restrictions.sqlRestriction(word REGEXP '^[A-Z][A-Za-z]*$')
Please check the link which will be helpful for this case: Regular expression with criteria
In Oracle, if you name a table User, you must query the table by putting quotes around the word user.
This will not work
select * from User
This will work
select * from "User"
My question is, how do I run a hibernate HQL query on a table named User? I have tried putting "" around User, escaping quotes, single quotes, nothing works. HQL doesn't like those characters and errors out. I have googled and searched for a solution and found nothing.
You need to escape the table name, in your entity mapping:
#Entity
#Table(name="`User`")
public class User {
...
}
The when you write an HQL query like this:
from User
Hibernate will generate an SQL query like this:
select * from "User"