I find this thread very helpful, but it looks like implementation in the advices are not quite efficient because of the nature of JPA.
I am looking for a solution to pickup latest entry of joining query with grouping, implemented by JPA, so it is not a simple job. My implementation is put "ORDER BY time DESC" in the end and pick up the first from return collection, instead of using MAX() function which have to introduce subquery as well, but I wonder is this a good alternative ?
This is the complex query I made by the example from the other example:
"SELECT oo FROM Order AS oo WHERE oo.id IN " +
"(SELECT temp.id FROM " +
"(SELECT t.order.id AS id, MAX(t.order.orderTime) AS ordTime FROM Transaction t " +
"WHERE t.order.name= :name " +
"GROUP BY t.order.name) AS temp" +
")";
This is the query which I think could be a good alternative but not sure:
String query = "SELECT t.order FROM Transaction AS t " +
" WHERE t.order.name= :name " +
" ORDER BY t.order.orderTime DESC";
// and simply just pick up the 1st as the latest entry from result:
Order order = em.createQueryquery , Order .class).getResultList().get(0);
Problem with your solution is that it will load ALL entries in a list for you to select one. As per this answer, you need to limit result to 1 by calling query.setMaxResults(1). Other than that, it is a perfectly fine solution. You can also use query.getSingleResult() to get single entity, but beware of NoResultException if there are no such records.
Related
I have the following JPQL query in my repository and when I run its SQL version (our database is PostgreSQL), it works properly. However, when I run this JPQL, it returns only the records that has True value. I think the problem may be related to JPQL and wanted to ask. So, is there any problem regarding to this query?
#Query("SELECT p.uuid as uid, " +
"CASE WHEN m.uuid IS NOT NULL THEN true ELSE false END as d FROM Product p " +
"LEFT JOIN Menu m on m.productUuid = p.uuid ")
List<ProductMenu> findAllProduct();
The search query perfectly works, but the query result is case sensitive.
How do I resolve this?
You may try to apply LOWER function on the query and, at the Java side, convert the variable search to lower case as well.
Your code should look like as follow:
ResultSet rs = st.executeQuery("select * from product where LOWER(name) like '%" + search.toLowerCase() + "%' or LOWER(category) like '%" + search.toLowerCase() + "%' and active='yes'");
However, for a matter of performances, I recommend that you create an index over the fields "name" and "category", if it's not already a composite key of the table "product".
Moreover, be careful of a possible NullPointerException which the statement search.toLowerCase() can cause.
try to apply Lower Function on the query Definitely, you will get a positive result
I have this native query:
"SELECT DATE_FORMAT(createdDate, \"%Y-%M\") AS open_month, :" + filter
+ " AS filterName, COUNT(id) AS counts from cases group by open_month, :"
+ filter + " ;"
And then use setResultTransformer and Transformers.aliasToBean to convert to my DTO.
In database, Feb has two records using the query that Hibernate print out
SELECT
DATE_FORMAT(createdDate, "%Y-%M") AS open_month,
? AS filterName,
COUNT(id) AS counts
FROM
cases
GROUP BY
open_month,
?;
But when I get query.list(), 2017-Feb has only one record and the counts is 5, meaning that two records are merged.
Does anyone know why is this and how to return the exact result?
It seems like the same parameter will break the query like this: Using hibernate named parameter twice
If I use different parameter names then it works fine
I'm using Spring Boot, and trying to utilize HQL to set up a LEFT JOIN between 3 tables.
The three entites I have are Usage, SubscriptionPeriod and Subscription. Necessary Hibernate annotation have been set in the entity classes.
Usage and SubscriptionPeriod : Many to One (a SubscriptionPeriod can have multiple Usage)
Subscription and SubscriptionPeriod : Many to One (a Subscription can have multiple SubscriptionPeriod).
Goal : I want to fetch all the usages by providing a subscription id and usage has title 'NEW'. And, I want to do it in a single database request
I have:
String hql =
"SELECT DISTINCT u " +
"FROM Usage u " +
"LEFT JOIN u.subscriptionPeriod p " +
"LEFT JOIN p.subscription s " +
"WHERE s.remoteId = :remoteId AND u.title='NEW'";
Currently, there seems to be a syntax error around "FROM Usage ". It reads " can not resolve symbol Usage"
How can I achieve what I want? I am open to listen to alternative solutions as well. Thanks.
I was probably thinking it in a probably more convoluted way. This #Query on a method works great. Hope this will be useful for somebody.
#Query(
"SELECT u " +
"FROM usages u " +
"LEFT JOIN u.subscriptionPeriod p " +
"LEFT JOIN p.subscription s " +
"WHERE u.paymentStatus = 'NEW' AND s.remoteId = :remoteId"
)
public List<Usage> find(#Param("remoteId") String param)
I am trying to use jpa with join to qry one to many entities
Entity Table Unit (one)
Entity Table Appartment (many)
I am using the following JPA
#Query("Select u from Unit u inner join u.appartmentList a " +
"where u.unitNumber IN :unitNumbers " +
"and a.appNumber= :appNumber")
Page findApt(#Param("unitNumbers") Collection<String> unitNumbers,
#Param("appNumber") Integer appNumber,
Pageable pageable);enter code here
I need to be able to get 2 appartments (one in each unit passed as unita,unitb).
if I do a GET for ../findStuff?unitNumbers=unita,unitb&appNumber=1
But I get all appartments in each unit instead, not just appNumber=1 in each unit.
Any idea on how to achieve this would be greatly appreciated
Thank you
You need to query on Appartment.
#Query("select a from Appartment a where" +
" a.appNumber= :appNumber and a.unit.unitNumber IN :unitNumbers"
If unit is lazy fetched, you may want to fetch it with the first query, so the query becomes:
#Query("select a from Appartment a join fetch a.unit where" +
" a.appNumber= :appNumber and a.unit.unitNumber IN :unitNumbers"