Apply native SQL where clause to Nhibernate query for entity - sql

I have this problem.
I have a module (module 1) that use Nhibernate to manage entity persistence; this module interacs with an other module (module 2).
The "module 2" allows to generate dynamically native SQL where clause. Now I would use it to manage filter operation in "module 1".
Which is the bast way to do it?
Is possible get the native SQL Select from "Nhibernate" entity without write manually it?
Then, if I get the native SQL Select statement I can easily apply where. Is there a better way?
Otherwise, is possible translate navite SQL statement to HQL statement?

i don't exactly understand your problem but it seems to me that filters is what you want

Really I had a old procedure that build where clause and return it in SQL Native Format (according to specific preset)...now to solve my problem I have modified the procedere to obtain the where clause in HQL Format so apply it to my entity. So it works.

Related

Mulesoft not able to pass dynamic SQL queries based on environments

Hello for demonstration purposes I trimmed out my actual sql query.
I have a SQL query
SELECT *
FROM dbdev.training.courses
where dbdev is my DEV database table name. When I migrate to TEST env, I want my query to dynamically change to
SELECT *
FROM dbtest.training.courses
I tried using input parameters like {env: p('db_name')} and using in the query as
SELECT * FROM :env.training.courses
or
SELECT * FROM (:env).training.courses
but none of them worked. I don't want my SQL query in properties file.
Can you please suggest a way to write my SQL query dynamically based on environment?
The only alternative way is to deploy separate jars for different environments with different code.
You can set the value of the property to a variable and then use the variable with string interpolation.
Warning: creating dynamic SQL queries using any kind of string manipulation may expose your application to SQL injection security vulnerabilities.
Example:
#['SELECT * FROM $(vars.database default "dbtest").training.courses']
Actually, you can do a completely dynamic or partially dynamic query using the MuleSoft DB connector.
Please see this repo:
https://github.com/TheComputerClassroom/dynamicSQLGETandPATCH
Also, I'm about to post an update that allows joins.
At a high level, this is a "Query Builder" where the code that builds the query is written in DataWeave 2. I'm working on another version that allows joins between entities, too.
If you have questions, feel free to reply.
One way to do it is :
Create a variable before DB Connector:
getTableName - ${env}.training.courses
Write SQL Query :
Select * from $(getTableName);

How do I clear all the rows from a table using Entity Framework Core?

New to Entity Framework Core. How do I clear all the rows from a table?
I searched around and found the following solution:
_context.Database.ExecuteSqlCommand("TRUNCATE TABLE[TableName]");
Unfortunately using ExecuteSqlCommand throws a compiler error.
Am I missing a namespace or is there another way to do this?
Thanks, JohnB
ExecuteSqlCommand is Obsolete,you can see the details here.
For the execution of SQL queries using plain strings, use ExecuteSqlRaw instead. For the execution of SQL queries using interpolated string syntax to create parameters, use ExecuteSqlInterpolated instead.
So you can use:
_context.Database.ExecuteSqlRaw("TRUNCATE TABLE[TableName]");

For update simulation in oracle using NHiberante

Can any one tell me how to implement for update in oracle using NHibernate? Are there any pre defined clauses for this? If not please tell me how can I implement it.
Read this chapter.
Basically, you need to get your poco using ISession.Get(Of Poco)(id, LockMode.Upgrade).
As a result, that should issue a SELECT ... FROM Poco Where Id = ? FOR UPDATE.
You can also invoke using LockMode.UpgradeNoWait to get a equivalent FOR UPDATE NOWAIT SQL Statement

Expression Builder to SQL Query

I know, that similiar post was posted already (SQL query from Toplink expression), but I didnt find an answer there. I would like to get SQL query from Expression Builder expression:
I have
Expression exp = builder.get(NUMBER.getAttributeName()).equal(getNumber());
and I want to see the SELECT statement, like (Select * from table WHERE number=....)
Or is it possible to execute the expression from Expression Builder without session? (I know that when I used query.prepareCall(session, new DatabaseRow()) I can obtain statement, but I just need to avoid using session. Thank you very much.
You must have the Session (what are you going to execute it on without a Session???).
query.prepareCall(session, new DatabaseRow())
This is how you get the SQL, you need to create a ReadAllQuery with the expression to be able to get the SQL (an Expression is just a where clause).
The problem was, that i wasnt able to create session because I couldnt register my project. And that was because I miss one line of code in my Project.class
setName(APPLICATION_NAME);
After this, I was able to create session and execute query. Thank you anyway

How can I change column length using HQL query?

I tried session.createSQLQuery("ALTER TABLE People MODIFY address VARCHAR(1000);").executeUpdate();
but this throws org.hibernate.exception.SQLGrammarException: could not execute native bulk manipulation query
After a lot of googling, the recommendation is to use HQL instead of SQL query to do bulk updates. Not sure how to use HQL to accomplish this. There seems to be no decent HQL documentation for updating column length in a table.
Thanks so much for the help.
I suggest you to run this native SQL query via session.connection().
See section 16.2.2.1. Rules/limitations for using stored procedures of Chapter 16. Native SQL, it's near your ALTER query.
All the rest depends on your database vendor. Good Luck!
I could not find a way. Looks like a limitation. I added a new field and copied over data from old field at start-up!