Concatenation in JPA 1.0 - java

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!

Related

Cannot return boolean using JPQL in Spring

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();

Hibernate native SQL does not return correct result comparing to database

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

finding latest entry by JPA

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.

Hibernate generates error SQL like ".=."

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.

JPA query with filter on current time stamp - seconds for Oracle

I am using JPA, Eclipse link, Oracle.
I am trying to introduce a condition in my JPA query equivalent to following oracle expression
current_timestamp - NUMTODSINTERVAL(10, 'SECOND')
I have tried several queries similar to following
select u from User u where (current_timestamp - FUNC('NUMTODSINTERVAL', :offset, 'SECOND')) > u.birth_date
but to no avail. All result in syntax errors. I want to avoid native query as well as calculating values using java.
I found following on some oracle website.
Use the add_months function in Oracle, and use simple math to convert the month value to seconds.
If above is possible then I'll be able to use following
Query query = entityManager.createQuery("select u"
+ " from User u"
+ " where"
+ " FUNC('add_months', CURRENT_TIMESTAMP, :offset) > u.birth_date "//)"//
);
query.setParameter("offset", getOffSet());
Edit: I need some Eclipse link JPA way of doing this or Oracle way using which add_months can be used to add/deduct seconds.
(Standard JPA 2.1) JPQL allows "FUNCTION" to be included in WHERE clauses. Nowhere is there a "FUNC", except maybe in some vendor extension, and people are always advised to avoid those particularly where there is a standard

Categories

Resources