Executing NOT IN query after LOAD DATA INFILE - java

I have successfully insert data with LOAD DATA INFILE. After that when I am using the query:
SELECT * FROM tempupload
WHERE columnName NOT IN (SELECT columnName FROM othertable)
Not giving me the desirable result. But when I convert columnName datatype to double (and after again to varchar) giving me desirable results.
Please guide me as I have to use these queries from my Java EE Application.

If you have any NULLs in your othertable you may have some issues with a NOT IN (see this). To improve your query you could try:
SELECT * FROM tempupload
WHERE EXISTS (SELECT 1
FROM othertable
WHERE othertable.columnName = tempupload.columnName)

Related

Can we modify the query using Statement Inspector after binding the parameters to query

Getting the query in Statement Inspector after the parameters are binded.
I am following the below links for statement inspector, can the query be modified after the parameters are binded.
https://vladmihalcea.com/hibernate-statementinspector/
https://stackoverflow.com/questions/39112308/how-i-can-configure-statementinspector-in-hibernate
For eg:
Query q = entityManager.createNativeQuery("Select * from employees where address2_id = ?");
q.setParameter(1, address2_idval);
For the above scenario if i log the query using inspector it's printing
Select * from employees where address2_id = ?
the reason i am using inspector is sometimes address2_idval can be empty(very rare case) at that time it's generating Select * from employees where address2_id = '' where address2_id is a numeric datatype in Db(POSTGRES).
I am getting the following error invalid input syntax for type numeric "".
What i am planning on implementing in inspector is if i can get Select * from employees where address2_id = '' like this in inspector block i can check with regex and replace = '' with is null.
Can anyone pls suggest if this is possible?
NOTE: We are migrating from oracle to potgres so there are tons of queries which might face this issue, so planning on implementing in one place which intercept all the queries with this scenario

Delete from table on same select same table mariadb using jpa

I need delete from table on operation of same table .JPA query is
DELETE FROM com.model.ElectricityLedgerEntity a
Where a.elLedgerid IN
(SELECT P.elLedgerid FROM
(SELECT MAX(b.elLedgerid)
FROM com.model.ElectricityLedgerEntity b
WHERE b.accountId='24' and b.ledgerType='Electricity Ledger' and b.postType='ARREARS') P );
I got this error:
with root cause org.hibernate.hql.ast.QuerySyntaxException: unexpected
token: ( near line 1, column 109 [DELETE FROM
com.bcits.bfm.model.ElectricityLedgerEntity a Where a.elLedgerid IN (
SELECT P.elLedgerid FROM ( SELECT MAX(b.elLedgerid) FROM
com.bcits.ElectricityLedgerEntity b WHERE b.accountId='24'
and b.ledgerType='Electricity Ledger' and b.postType='ARREARS') P ) ]
at
org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at
org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at
org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
at
org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:284)
Same query is running on mysql terminal ,but this is not working with jpa .Can any one tell me how i can write this query using jpa .
I don't understand why do you use Pbefore the last parenthesis...
The following code is not enough ?
DELETE FROM com.model.ElectricityLedgerEntity a
Where a.elLedgerid IN
(SELECT MAX(b.elLedgerid)
FROM com.model.ElectricityLedgerEntity b
WHERE b.accountId='24' and b.ledgerType='Electricity Ledger' and
b.postType='ARREARS')
Edit for bypassing mysql subquery limitations :
The new error java.sql.SQLException: You can't specify target table 'LEDGER' for update in FROM clause
is known in mysql when you use it with JPA. It's one MySQL limitation.
A recent stackoverflow question about it
In brief, you cannot "directly" updated/deleted a table that you query in a select clause
Now I understand why your original query did multiple subqueries seemingly not necessary (while it was useful for mysql) and had a "special" syntax.
I don't know tricks to solve this problem in JPA (I don't use the MySQL DBMS for a long time now).
At your place, I would do two queries. The first where you select the expected max elLedgerid and the second where you could delete line(s) with the id retrieved in the previous query.
You should not have performance issues if your sql model is well designed, the sql indexes well placed and the time to access to the database is correct.
You cannot do this in a single query with Hibernate. If you want to delete the max row(s) with Hibernate you will have to do so in two steps. First, you can find the max entry, then you can delete using that value in the WHERE clause.
But the query you wrote should actually run as a raw MySQL query. So why don't you try executing that query as a raw query:
String sql = "DELETE FROM com.model.ElectricityLedgerEntity a " +
"WHERE a.elLedgerid IN (SELECT P.elLedgerid FROM " +
"(SELECT MAX(b.elLedgerid) FROM com.model.ElectricityLedgerEntity b " +
"WHERE b.accountId = :account_id AND b.ledgerType = :ledger_type AND " +
" b.postType = :post_type) P );";
Query query = session.createSQLQuery(sql);
query.setParameter("account_id", "24");
query.setParameter("ledger_type", "Electricity Ledger");
query.setParameter("post_type", "ARREARS");
Just want to extend existing answer:
In brief, you cannot "directly" updated/deleted a table that you query in a select clause
This was lifted with starting from MariaDB 10.3.1:
Same Source and Target Table
Until MariaDB 10.3.1, deleting from a table with the same source and target was not possible. From MariaDB 10.3.1, this is now possible. For example:
DELETE FROM t1 WHERE c1 IN (SELECT b.c1 FROM t1 b WHERE b.c2=0);

How to order by a specific column a MySQL query with Java and Hibernate?

I'm trying to use the same query to sort my table columns but when I pass as a parameter the column to ORDER BY, it adds quotes before and after my column name. If you are using ORDER BY parameter, the column name have to be written without being between quotes or MySQL is going to ignore it.
Example or query to execute:
select * from app_user ORDER BY mobile_token ASC LIMIT 0 , 20
This is what hibernate send to MySQL:
select * from app_user ORDER BY 'mobile_token' ASC LIMIT 0 , 20
Java query:
query = JPA.em().createNativeQuery("select * from app_user ORDER BY :column ASC LIMIT :init , :page",AppUser.class);
query.setParameter("column", column);
query.setParameter("init", pageNumber*pageSize);
query.setParameter("page", pageSize);
I could change the NativeQuery by:
"select * from app_user ORDER BY "+column+" ASC LIMIT :init , :page"
but this is going to become my app unsafety.
You can only pass values as parameters to a query. Not column or field names. That would make it impossible for the database to know which columns are actually used in the query, and thus make it impossible to prepare the execution plan.
So your solution using concatenation is the only one. Just make sure the column doesn't come from the user. Or if it comes from the user, that it's a valid column name and that the user is allowed to use it.

Not able to get Updatable and scrollable Result set in Oracle using Java

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn=DriverManager.getConnection(URL,username,password);
String sql="select * from test where user_id='abc'";
stmt=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE);
ResultSet rs=stmt.executeQuery();
rs.afterLast();
getting exception in this line, afterLast operation not allowed .
Reading from the oracle documentation:-
To produce a scroll-sensitive result set: A query cannot use SELECT * .
However, there is a workaround for this.
As a workaround for the SELECT * limitation, you can use table aliases, as shown in the following example:
SELECT t.* FROM TABLE t ...
Change your query to select test.* from test where user_id='abc'
or use specific column names to retrive instead of *.

How to get Column names Column Data types Form Table using Eclipse Link/JPA

I am working on JPA. My requirement is get the Column Name and Data types from Table.
I have Query's to do that but those are Native Query's. If I used those Native Query's, Is It will support on any Data Base like Oracle, MySql,......
Now am Using MySql with JPA working fine.
Below Query for Getting Table Column Names
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'SchemaName'
AND TABLE_NAME = 'TableName';
I executed above Query using createNativeQuery() in JPA . Is it will support in all Data Bases. If not then how can I do this. Thank you very much.
In Oracle you can use ALL_TAB_COLUMNS table and in MS SQL server and MySQL you can use INFORMATION_SCHEMA.COLUMNS table to get information about tables and columns.
SELECT * FROM ALL_TAB_COLUMNS
WHERE OWNER = 'SchemaName' AND TABLE_NAME = 'TableName';

Categories