I have a field in SSMS which is of type nvarchar. It has date and time stored in it . I want to retrieve records which is before a date using criteria_builder. I am able to retrieve it using SQL. The Sql is given below:
SELECT CONVERT(DateTime,f.field_values,103) as DueDate
FROM TableA a
JOIN TableB p ON a.id = p.activity_id
JOIN TableC s ON p.id = s.package_id
JOIN TableD f ON s.id = f.package_section_id
WHERE a.code like '%sass%' and f.field_name='Due_Date'
However when i try to use Hibernate criteria:
I get
Conversion failed when converting date and/or time from character string.
This occurs as I m not using the SQL Convert function. I have searched a lot online but unable to find how can i use CONVERT in Hibernate criteria. Hibernate predicate I am using is:
Predicate fieldPredicates = cb.and(cb.equal(packageFieldJoin.get(TableB_.fieldName), "Due_Date"),
cb.lessThan(packageFieldJoin.get(TableB_.fieldValuesData)), sdf1.parse(CLOSE_DUE_BEFORE)))
Related
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 came into a situation where i want to select rows which meets within some date.
i am using oracle and my query looks like :-
Query
SELECT d.id,d.ADVERTISEMENTCODE,d.LASTDATEOFFORMSUBMISSION,e.LASTDATEFORFORMSUBMISSION,
d.studentMasterId FROM CANDIDATEAPPEARAGAINSTADVTCODE d LEFT OUTER JOIN ADVERTISEMENTCODE e
ON d.ADVERTISEMENTCODE = e.ADVERTISEMENTCODE WHERE paymentStatus='Pending'
and studentMasterId='8670' and
TO_DATE(e.LASTDATEFORFORMSUBMISSION,'dd/mm/yyyy')+4 > TO_DATE('11/05/2017','dd/mm/yyyy');
this is a running query and it returns exactly what i want.
but now i want to do it with hibernate query language. how can it be done?
I have a query like below:
select * from Products p where ? between p.effctDate and p.expDate
Can I translate the above query to a spring data jpa query method? Like for example:
findByProductId(productId)
or I only have to use #Query or a Named query to handle such types of queries. I tried searching here first before asking the question as well as spring data site but did not find any solution. any help is appreciated.
You could use the following query:
Date date = //Input date
List<Product> = findByEffctDateAfterAndExpDateBefore(date, date);
Note that you have to enter date twice to match both 'where' clauses. This is under the assumption you are using Date objects, not literal Strings.
See JPA Repositories Table 2.3 for more info.
You can try to use something like below :
List findAllByDateApprovedBetween(String dateApprovedFrom, String dateApprovedTo);
It translates to :
Hibernate: select applicatio0_.id as id1_1_, applicatio0_.date_approved as date_app4_1_ from application applicatio0_ where (applicatio0_.date_approved between ? and ?)
You can try something like this trick:
select
p
from
Product p
where
(?1 = 'field1' and p.field1 between p.effctDate and p.expDate)
or (?1 = 'field2' and p.field2 between p.effctDate and p.expDate)
or (?1 = 'field3' and p.field3 between p.effctDate and p.expDate)
or (?1 = 'field4' and p.field4 between p.effctDate and p.expDate)
...
But this is not the best approach IMO...
To build dynamic queries you can use thees ones:
Specifications
Query by Example
Querydsl Extension
I've successfully created some JPA 2 criteria queries, but im now converting a query that is too complex for me to be able to convert it. Anyone up for a challenge? I will be eternally thankful for any response! :)
This is the query in plain SQL (mysql):
SELECT o.orderId, oi.insertDate
FROM AIDA_ORDER o
LEFT OUTER JOIN
(SELECT orderId, MIN(insertDate) AS insertDate FROM AIDA_ORDER_INSERT
WHERE insertDate > CURDATE() GROUP BY orderId) AS oi
ON oi.orderId = o.orderId
ORDER BY oi.insertDate, o.orderId;
The AIDA_ORDER table corresponds to a OrderBean, and AIDA_ORDER_INSERT to a OrderInsertBean.
A AIDA_ORDER can have multiple AIDA_ORDER_INSERTS.
I am trying to get distinct values from table application based on the column entry_id. I've managed to get this working with plain SQL:
SELECT DISTINCT ON (a.entry_id) a.* FROM application AS a
JOIN entry AS e ON e.id = a.entry_id
WHERE e.valid_until BETWEEN ? AND ?;
The problem is, I have to translate it to HQL. Is there any way to resolve it in HQL without Criteria API?
It looks like this feature has been requested a long time ago but is still unresolved.