Convert SQL to HQL? - sql

How can I convert the following SQL query into HQL?
SELECT count(sa.AID)
FROM A sa
, B sal,C m
WHERE sa.AID = sal.AID(+) and sa.BID = m.BID and sa.AID ='0001'

You need to transfer each table/column into it's associated Entity/Class in JAVA, then build the query with Hibernate ORM as below.
Suppose
- The entity name for the table sa is saEntity, for the table B is bEntity, and for the table C is cEntity.
- The class name for the column AID is AidClass, and for the column BID is BidClass
Then the Hibernate ORM query can be written as per the following (I like formating HQL queries inside annotations on multiple lines to make it easier to read & adapt).
#Query( "SELECT COUNT(sa.AidClass) "
+ "FROM saEntity sa, "
+ " bEntity sal "
+ " cEntity m"
+ "WHERE sa.AidClass = sal.AidClass"
+ " AND sa.BidClass = m.BidClass "
+ " AND sa.AidClass ='0001'")
public List <?> runMyQueryMethod();

Try looking at the answer to this question.
HQL to SQL converter
Or this article may help..
https://forum.hibernate.org/viewtopic.php?t=972441
If you showed some of the mappings I could probably help with the HQL. You could just use
Session.CreateSQLQuery instead??

Related

Query to select next few rows

I'm working on a search tool, the words (to be searched) are in object array
first i search the database(about 450k rows) using FTS4 and sqlite. This works fast, the SQL im using is:
String s = "select * from fts_sggs where word_text MATCH ? ";
but then, i need some extra words which are a part of the same table.
The FTS4 doesnt seem to work here.the sql is
String s ="select * from fts_sggs where word_number BETWEEN " + wordnumlo + " AND " + wordnumhi + " and page_number =" + pgnum;
im a newbie to SQL Programming, I need a single query that could perform both tasks fast. this is being done in java which is terribly slow.

How to compare if BigDecimal value is inside a range in a query?

Lets say that I have to check in a query if a value is inside a range, and this value is BigDecimal in Java. The values inside the database are numbers(with two decimals). The ideia is above...but I don´t know the right way and to make it a little worst, I don´t have access to test the query apart. I´m using jpa extended repository, so probably I need to write my own query in this case.
#Query("SELECT c "
+ " FROM Taxes c "
+ " WHERE c.minOrderValue <= 'Mvalue' AND c.maxOrderValue >= 'Mvalue' "
)
Optional<TaxesRR> getTaxes(#Param("Mvalue") BigDecimal Mvalue);
That´s how it´s worked for me. Any other solution is still appreciated.
#Query("SELECT c "
+ " FROM ConjuntoTaxasRetencaoRapida c "
+ " WHERE c.valorMinimoPorteFaturamento <= ?1 "
+ " AND c.valorMaximoPorteFaturamento > ?2")
Optional<ConjuntoTaxasRetencaoRapida> obterTaxasRR(BigDecimal valorProjetadoMes, BigDecimal valorProjetadoMesConsolidado);

Sql Case expression in JPA criteria update

I have a named query :
"UPDATE student SET student.marks = " +
" CASE WHEN student.name in :" + nameListOne +
" THEN 10 ELSE marks " +
" END , student.class = 6 WHERE student.name in :" + namelistOneAndTwo
how can i achieve this using criteria update.
This is just a example i want to understand how case expression can be used in criteria update.
Might be easier just to have two updates. Not sure what "nameListOne" is, I'm assuming this is some parameter in a dynamic SQL query string.
UPDATE student
SET marks = 10
WHERE name IN nameListOne;
UPDATE student
SET class = 6
WHERE name IN nameListOneAndTwo;
There's some value in keeping things simple and intuitive.

How to transform SQL query into JPQL query?

I'm looking for how to transform the following query (SQL) into query JPQL;
SELECT * FROM equipements eq
LEFT JOIN check_lists checks
ON eq.id_equipements = checks.equipements_id
LEFT JOIN responses_check_lists resp
ON checks.id_check_lists = resp.check_lists_id
AND resp.missions_id = 15
AND eq.id_equipements = 1
ORDER BY checks.id_check_lists
I followed the documents on the internet but I do not get the correct transformation of my query in JPQL.
I know that the attributes of the query will be replaced by the attributes of the class.
I posted here to help me in transforming the SQL query.
Edit1:
#Query(
"SELECT r, checks, eq"
+ " FROM Equipements eq"
+ " LEFT JOIN CheckLists checks "
+ " ON eq.idEquipements = checks.equipements.idEquipements"
+ " LEFT JOIN ResponsesCheckLists r"
+ " ON checks.idCheckLists = r.CheckLts.idCheckLists"
+ " AND r.Respmission.idMission= :idmiss "
+ " AND eq.idEquipements= :idEqp"
+ " ORDER BY checks.idCheckLists ASC"
)
Error of Edit1:
Caused by: java.lang.IllegalStateException: No data type for node:
org.hibernate.hql.internal.ast.tree.IdentNode +-[IDENT] IdentNode:
'r' {originalText=r}
org.hibernate.hql.internal.ast.InvalidPathException: Invalid path:
'checks.idCheckLists'
Thank you in advance,
Analysing your SQL and without see the entities, your JPQL can be something like this:
SELECT eq FROM Equipements eq
LEFT JOIN eq.checks check
LEFT JOIN check.responses resp
LEFT JOIN resp.missions mission WITH mission.id = :idmiss
WHERE eq.id = :idEqp
ORDER BY check.id
But, to be something like this, you need to adjust the mapping of your entities (entity name, column names, etc).

Giving parameter in Native sql in hibernate

Can someone please tell me how I can send parameter in native sql in hibernate
In the following code, i want to enter contract_id as a parameter.
List list=session.createSQLQuery("select {b.*},{p.*},{t.*} from bidtool.bt_boiler_plates b,bidtool.bt_profile p,bidtool.bt_trade_lane t "
+ "where b.contract_id=p.contract_id AND p.contract_id=t.contract_id AND ")
.addEntity("b",Boiler_Plates.class)
.addEntity("p",BidToolProfiles.class)
.addEntity("t",BidToolTradeLanes.class).list();
Your help will be appreciated. Thanks
Try:
List list=session.createSQLQuery("select {b.*},{p.*},{t.*} from bidtool.bt_boiler_plates b,bidtool.bt_profile p,bidtool.bt_trade_lane t "
+ "where b.contract_id=p.contract_id AND p.contract_id=t.contract_id AND contract_id=:contractId")
.addEntity("b",Boiler_Plates.class)
.addEntity("p",BidToolProfiles.class)
.addEntity("t",BidToolTradeLanes.class)
.setParameter("contractId", contractId).list();
Take a look on this..
Query query = session.createSQLQuery("select {b.*},{p.*},{t.*} from bidtool.bt_boiler_plates b,bidtool.bt_profile p,bidtool.bt_trade_lane t "
+ "where b.contract_id=p.contract_id AND p.contract_id=t.contract_id AND contract_id=:ID")
.addEntity("b",Boiler_Plates.class)
.addEntity("p",BidToolProfiles.class)
.addEntity("t",BidToolTradeLanes.class);
List list = query.setInteger("ID", 1234).list();