NHibernate HQL could not locate named parameter [parameterName] error - nhibernate

I am having problems with my HQL query bellow:
var merchantTransactions = session.CreateQuery("SELECT MS.Transaction "+
"FROM MerchantSite AS MS "+
"INNER JOIN MS.Transaction AS MST"+
"WHERE MS.Site.Name = :merchantName");
Then I set parameters like this:
merchantTransactions.SetParameter("merchantName", merchantName);
And it gives me a "could not locate named parameter" error, any ideas why?
merchantName does exist in this context and all the table names are correct.

You are missing a space between MST and WHERE.

You have to use methods to create an HQL statment using a fluent interface style. Try something like this:
var merchantTransactions = session.CreateQuery("SELECT MS.Transaction "+
"FROM MerchantSite AS MS "+
"INNER JOIN MS.Transaction AS MST"+
"WHERE MS.Site.Name = :merchantName")
.SetParameter("merchantName", merchantName);
Or
merchantTransactions = merchantTransactions.SetParameter("merchantName", merchantName);
And do not forget to call a method to concrete your query.
var result = merchantTransactions.List<Entity>();

Related

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).

Order of JSON returned by ORMLite?

I'm new to web services and trying to query some tables using ORMLite, it doesn't support join statements so I'm using a raw query. I was wondering if there is a way to specify how the JSON is returned. What I have right now is:
Dao<CodesModel,String> CodesDao = DaoManager.createDao(connectionSource, CodesModel.class);
GenericRawResults<String[]> rawResults =
CodesDao.queryRaw(
"select r.CodeA, s.SubCodeA, r.CodeB, s.SubCodeB " +
"from CodesTable r JOIN SubCodesTable s ON s.CodeA = r.CodeA " +
"where SubCodeB = '" + b_sub + "' AND r.CodeB = '" + b_code + "'");
And the results are returned as a String[] and always seem to be in the order of
[CodeA, SubCodeA, CodeB, SubCodeB]
but I have only tested this locally and can't find in the documentation what determines the order for variables in the array that is returned.
The results are ordered that way because that is the order in which you specified them in the select statement. If you want the results ordered in a different way, reorder them in your query.
If anyone is looking for a way to know column names when using "select *" you can also use "getColumnNames()" on the rawResults object and they will always be in the order that the results are. Examples
//The result is returned as a GenericRawResults object
List<String[]> results = rawResults.getResults();
String[] columns = rawResults.getColumnNames();
JSONObject obj = new JSONObject();
if(results.size()>0)
{
obj.put(columns[0], results.get(0)[0]);
obj.put(columns[1], results.get(0)[1]);
obj.put(columns[2], results.get(0)[2]);
obj.put(columns[3], results.get(0)[3]);
}

How to use a variable inside an sql query

I have a query such this (i put it into a string for preparation of the query itself):
select r.id,
r.id_user,
r.date_pubblication,
r.name,
r.description,
r.type,
u.name as Uname
from resources r,
users u
where r.id_user=id_logged
Now all these fields in the select are field of DB tables . Only id_logged is not a field of a table but is a java variable defined by this way:
User user = userService.getUserCurrent();
long id_logged = user.getId();
The error returned after running the query is
ORA-00904: "ID_LOGGED": invalid identifier
Something more to say is that when I define id_logged by giving the value user.getId(), there is a warning
- The value of the local variable id_logged is not used
Can you explain what is my mistake? I can I run this query correctly ?
So If I understand correctly, you generate somtehing like this:
"select r.id, r.id_user, r.date_pubblication,r.name, r.description, r.type, u.name as Uname from resources r, users u where r.id_user=id_logged"
as a string ?
If so - check the last parameter: r.id_user=id_logged.
It is a string. And you declare the id_logged as a javascript variable.
If so - you have to join the string of your query , with the variable value.
For example in:
javascript:
"query string" + id_logged
php:
"query string" . id_logged
This should help you - IF i understood your question correctly.
If your query is a Java string, you need to do something like this:
String id_logged = //Whatever you do to get the value of id_logged.
String query = "select r.id, "+
"r.id_user, "+
"r.date_pubblication,"+
"r.name,"+
"r.description, "+
"r.type, "+
"u.name as Uname "+
"from resources r, "+
"users u "+
"where r.id_user="+id_logged ;
//Now you can execute the query.
Be aware that this is prone to SQL injection. You really should be doing this sort of thing with prepared statements.

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

Convert SQL to HQL?

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??