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();
Related
I have the following Problem. I want to filter on a column in my table to either be true or if false then only give me the entries with a date greater than xxx. So basicly return me every Customer that is a valid customer and return alle Customers that are not valid and have a activationDate greater then 3.5 years.
name
is_customer
activation_date
Pete
True
2021.02.02
Sam
False
2021.02.02
I tried the query inside my psql console and there everything is working fine. But when im trying to adapt it to be used with #NamedQueries in my JPA Entity the query cant be validated (neither in Intellij nor while running the code).
Query in Postgres (WORKING FINE):
SELECT * FROM customer c
WHERE (c.is_customer = true) OR (c.is_customer = false AND c.activation_date > CURRENT_DATE + INTERVAL '3.5 years');
Query in Java (NOT WORKING):
#NamedQuery(name = CustomerBE.NAMED_QUERY,
query = "SELECT c FROM CustomerBE c "
+ "WHERE (c.isCustomer= true) OR (c.isCustomer= false AND c.activationDate > CURRENT_DATE + INTERVAL '3.5 years')"
)
Intellij Error: '(', <expression> or identifier expected, got '('
JPQL Error:
Exception Description: Syntax error parsing [SELECT c FROM CustomerBE c WHERE (c.isCustomer= true) OR (c.isCustomer= false AND c.activationDate > CURRENT_DATE + INTERVAL '3.5 years'].
[41, 297] The expression is not a valid conditional expression.
[297, 298] The query contains a malformed ending
I already tried adaption the bracket placement.
Did someone maybe have a simmilar problem or a workaround solution for me?
You probably need to put a space before the WHERE clause or else your string will not be a valid JPQL:
"SELECT c FROM CustomerBE cWHERE (c.isCustomer= true) O ..."
I'm not sure if this is the problem you reported, but it is also a problem to be fixed.
Ok fixed it now. The Problem was the postgre Part "CURRENT_DATE + INTERVAL '3.5 years'" had to substitute it with :filterDate and set it through the parameter.
I am trying to join 3 tables to get an output. Following are the tables
player_match(match_id,team_id,player_id)
player(player_id,name,......)
team(team_id,name......)
All the tables have equivalent classes assigned.
Following is the SQL query that I am running and getting the correct results.
select * from player_match M
inner join team T
on M.team_id = T.team_id
inner join player P
on P.player_id = M.player_id
where M.match_id = 335987;
I am running the following Named query in PlayerMatch java class. PlayerMatch class has Match and Team objects in it. Both of these objects are mapped #ManyToOne.
#NamedQuery(name="getMatchData",query="select PM from PlayerMatch PM "
+ "inner join Team T on PM.teamId = T.teamId "
+ "inner join Player P on PM.playerId = P.playerId "
+ "where PM.matchId = :matchID")
When I run the above hibernate query I get 22 results which is correct, but the contents of all the results are the same.
The SQL query that I have mentioned above returns 22 non duplicate rows.
I think I am messing up somewhere in the Hibernate query but can't figure out where.
Your HQL query is structured similarly to an SQL query. You shouldn't need to specify the 'on' conditions as they are derived from the hibernate schema.
See hibernate documentation for more info on hql joins:
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-joins
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"
I am facing an issue while trying to concatenate column values in JPA 1.0 named query :
SELECT aum.id.pin, aum.id.planNBR, aum.id.subPlanNBR, AVG(aum.aumAmount)
FROM AUMBalanceDO aum
WHERE " + "CONCAT(aum.id.pin,aum.id.planNBR,aum.id.subPlanNBR) NOT IN (
SELECT CONCAT(PAP.pin,edo.planNBR,edo.subPlanNBR)
FROM " + "ParticipantAdvicePortfolioDO PAP, EnrollmentDO edo
WHERE PAP.userID = edo.userID AND edo.status ='E'
AND edo.startDT < :endDate " + "
AND (edo.endDT > :endDate OR edo.endDT IS NULL)
)
AND aum.id.priceDate
BETWEEN :startDate AND :endDate GROUP BY aum.id.pin, " + "aum.id.planNBR, aum.id.subPlanNBR"
I am getting the below exception while trying to run the query,
unexpected token [concat]
As per the error its clear that in JPA 1.0 we dont have concat function is there any other alternative way to achieve this?
Plain SQL Native concatenation '||' seems to be working just fine in JPQL of JPA 1.0 spec (JPA is provided here by Hibernate 3.3.x and RDBMS is Oracle 11).
SELECT NEW PartSearchView(partIssue.id, partIssue.stage || partIssue.issue)
FROM PartIssue partIssue INNER JOIN partIssue.part part
The signature of constructor used above is:
PartSearchView(Long id, String stageIssue)
I got a nice concatenation of both stage and issue fed into PartSearchView constructor.
Hope this helps!
Cheers!
I have 3 tables in Oracle DB which relationship is #ManyToMany. So I have 2 significant tables and one for mappings.
I create a classes with name (if you want I can show my classes) named Entities, Keywords (I understand that naming is not correct but this is not my project I only do optimizations).
I use hibernate version 4.3.4.
I write query like this:
session = HibernateUtil.getSessionFactory().openSession();
String sql = "SELECT DISTINCT r FROM Rules r, Entities e " +
" WHERE r.entities = e.rules " +
" AND e IN :entities ";
Query query = session.createQuery(sql);
query.setParameterList("entities", entitiesList);
List<Rules> rulesList = query.list();
BUT! Hibernate generate strange SQL
Hibernate:
select
rules0_.rule_id as rule_id1_11_,
rules0_.rule as rule2_11_
from
rules rules0_,
entities entities1_,
rules_entities entities2_,
entities entities3_,
rules_entities rules4_,
rules rules5_
where
rules0_.rule_id=entities2_.rule_id
and entities2_.entity_id=entities3_.entity_id
and entities1_.entity_id=rules4_.entity_id
and rules4_.rule_id=rules5_.rule_id
and .=.
and (
entities1_.entity_id in (
? , ? , ? , ?
)
)
When I try to execute this query I receive that error:
java.sql.SQLException: ORA-00936: missing expression
When I copy this query to OracleDevepoler he didn`t like this expression "and .=.". Without that query executes correct.
What am I doing wrong ?
Maybe you used bad join in your query? From context i conclude that you should use something like that:
"SELECT DISTINCT r FROM Rules r inner join r.entities e " +
" WHERE e IN :entities ";
I think the correct query could be
select distinct e.rules from Entities where e.entityId in :entities
This is if Keywords is your join table and you have a collection of rules in Entities
If it isn't, can you show the mappings please, it could help.