Hibernate Data Exception in T-SQL query (Java Web application) - java
I have problem with executing the following T-SQL script in Java application using hibernate sessionFactory, however, this SQL code works when I run it in PostgreSQL:
...
query = session.createSQLQuery(
"; CREATE TEMP TABLE deathuntil1 AS "+
" SELECT id, jamoatorgunit, organisationunitid "+
" , REGEXP_REPLACE( "+
" COALESCE( "+
" substring( "+
" CAST(age(deathdate,birthdate) AS character varying ) "+
" from 1 for "+
" CASE WHEN (position('years' in CAST(age(deathdate,birthdate) AS character varying ))-1)>0 THEN position('years' in CAST(age(deathdate,birthdate) AS character varying ))-1 ELSE 0 END "+
" ) "+
" , '0'), '[^0-9]*' ,'0')::integer AS years "+
" FROM public.cro_death "+
" WHERE "+
" length(firstname)>= 3 "+
";"
);
query.executeUpdate();
the following is the error from Eclipse console:
* ERROR 2016-08-16 11:36:18,753 No value specified for parameter 1. (SqlExceptionHelper.java [qtp1843774775-20])
* ERROR 2016-08-16 11:36:18,760 Error while executing action (ExceptionInterceptor.java [qtp1843774775-20])
org.hibernate.exception.DataException: could not execute statement
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:135)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:124)
Caused by: org.postgresql.util.PSQLException: No value specified for parameter 1.
at org.postgresql.core.v3.SimpleParameterList.checkAllParametersSet(SimpleParameterList.java:225)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:190)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:424)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:161)
at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:133)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:186)
... 124 more
When running this sql through java, there is a "misunderstanding" of :: casting.
Please replace '[^0-9]*' ,'0')::integer with '[^0-9]*' ,'0')\\:\\:integer and it should work.
Related
H2 issues : request works in H2 console, not in jUnit test
I encounter a problem with H2 Database, I can't figure out what this problem is. I'm working on a 3 alphanums code generator that should behave by incrementing by one the highest existing code (this is a SQL function that will be stored on the DB server). The following code works perfectly in H2 console : SELECT TOP 1 concat(a.Chr, b.Chr, c.Chr) AS REF FROM (VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z')) a(Chr) CROSS JOIN (VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z')) b(Chr) CROSS JOIN (VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z')) c(Chr) WHERE concat(a.Chr,b.Chr,c.Chr) > ( SELECT TOP 1 CASE WHEN INSTRUCTION_CODE IS NULL THEN '' ELSE INSTRUCTION_CODE END FROM ACCOUNT ORDER BY INSTRUCTION_CODE DESC ) ORDER BY REF; I need to implement this request in a Java jUnit test. Here is what I did : public static ResultSet getReference(java.sql.Connection con) throws SQLException { String query = "SELECT TOP 1 concat(a.Chr, b.Chr, c.Chr) AS REF " + "FROM " + "(VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z')) a(Chr) " + "CROSS JOIN " + "(VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z')) b(Chr) " + "CROSS JOIN " + "(VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z')) c(Chr) " + "WHERE concat(a.Chr, b.Chr, c.Chr) > " + "(SELECT TOP 1 " + " CASE WHEN INSTRUCTION_CODE IS NULL " + " THEN '' " + " ELSE INSTRUCTION_CODE " + " END " + "FROM ACCOUNT order by INSTRUCTION_CODE DESC) " + "ORDER BY REF"; java.sql.ResultSet rs = con.createStatement().executeQuery(query); return rs; } Here's the error message I get when playing it : Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "SELECT TOP 1 CONCAT(A.CHR, B.CHR, C.CHR) AS REF FROM (VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z'))A([*]CHR) CROSS JOIN (VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z'))B(CHR) CROSS JOIN (VALUES('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z'))C(CHR) WHERE CONCAT(A.CHR, B.CHR, C.CHR) > (SELECT TOP 1 CASE WHEN INSTRUCTION_CODE IS NULL THEN '' ELSE INSTRUCTION_CODE END FROM ACCOUNT ORDER BY ACBS_PAYMENT_INSTRUCTION_CODE DESC) ORDER BY REF "; Is there anything you see I didn't ? Thanx
It looks like you use a recent version of H2 when you work with H2 Console, and some old version (1.4.196 or older) in your application. Such old versions don't support the derived column list syntax. You need to use a more recent version in your application too.
ORA-00933 SQL command not properly ended but good in SQL Developer
I am hoping someone can find what is the issue with my query because I am unable to see fault in it and Oracle SQL Developer seems to run the same query as the code in my Java Swing Application just fine. My query in SQL Developer: SELECT ad.ID,ad.Script_Name,ad.Current_Status, ad.Issues_found_during_run,ad.Testers, ad.Run_Date,ad.Tools,u.fTag,u.role, dbms_lob.substr(u.avatar) FROM allData ad INNER JOIN users u ON u.fTag = ad.lastUserWhoUpdated GROUP BY ad.ID,ad.Script_Name,ad.Current_Status, ad.Issues_found_during_run,ad.Testers, ad.Run_Date,ad.Tools,u.fTag,u.role, dbms_lob.substr(u.avatar) ORDER BY ad.ID ASC; Which run perfectly and returns the needed records I would be expecting it to. However, that same query in my Java Swing App does not seem to like it as it gives me the error of: java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended. My Java Swing App code: connectToDB(); String query = "SELECT " + "ad.ID," + "ad.Script_Name," + "ad.Current_Status," + "ad.Issues_found_during_run," + "ad.Testers," + "ad.Run_Date," + "ad.Tools," + "u.fTag," + "u.role," + "dbms_lob.substr(u.avatar) " + "FROM " + "allData ad " + "INNER JOIN " + "users u " + "ON " + "u.fTag = ad.lastUserWhoUpdated " + "GROUP BY " + "ad.ID," + "ad.Script_Name," + "ad.Current_Status," + "ad.Issues_found_during_run," + "ad.Testers," + "ad.Run_Date," + "ad.Tools," + "u.fTag," + "u.role," + "dbms_lob.substr(u.avatar) " + "ORDER BY " + "ad.ID;"; ResultSet rs = statement.executeQuery(query); ResultSetMetaData metaData = rs.getMetaData(); etc..etc.. My structure for those 2 tables is: SCRIPT_NAME VARCHAR2(100 BYTE) CURRENT_STATUS VARCHAR2(50 BYTE) ISSUES_FOUND_DURING_RUN VARCHAR2(150 BYTE) TESTERS VARCHAR2(30 BYTE) RUN_DATE DATE TOOLS VARCHAR2(20 BYTE) T_SUITE NUMBER(38,0) NOE2 VARCHAR2(5 BYTE) NOE3 VARCHAR2(5 BYTE) ID NUMBER(38,0) LASTUSERWHOUPDATED NUMBER DATELASTMOD DATE FTAG NUMBER(38,0) ROLE VARCHAR2(15 BYTE) AVATAR CLOB So, what could I be missing?
Remove semicolon after the ad.ID like below. You don't need it "ORDER BY " + "ad.ID";
create a function via nativeQuery
I'm trying to insert a function on database via native query, like this EntityManagerImpl entityManagerFunctionConta = (EntityManagerImpl) GermantechEntityManager.getEntityManager(); EntityTransaction transactionFunctionConta = entityManagerFunctionConta.getTransaction(); String functionConta = "CREATE OR REPLACE FUNCTION saldo_anterior_conta(dt_inicial date, id_conta bigint, id_empresa bigint) " + "RETURNS numeric AS " + "$BODY$ " + "declare " + "saldo_anterior numeric(14,2);" + "begin " + "select coalesce(sum(valor), 0) into saldo_anterior " + "from saldoinicialconta " + "where data < dt_inicial and empresa_id = id_empresa and conta_id = id_conta; " + "return saldo_anterior;" + "end;" + "$BODY$ " + "LANGUAGE plpgsql VOLATILE COST 100;" + "ALTER FUNCTION saldo_anterior_movimentacao(date, bigint, bigint) OWNER TO postgres;"; transactionFunctionConta.begin(); entityManagerFunctionConta.createNativeQuery(functionConta).executeUpdate(); transactionFunctionConta.commit(); but the following exception is thrown Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.BatchUpdateException: Entrada em lote 1 <unknown> foi abortada. Chame getNextException para ver a causa. Error Code: 0 at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324) at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeJDK12BatchStatement(DatabaseAccessor.java:882) at org.eclipse.persistence.internal.databaseaccess.DynamicSQLBatchWritingMechanism.executeBatchedStatements(DynamicSQLBatchWritingMechanism.java:144) at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.writesCompleted(DatabaseAccessor.java:1714) is there something wrong with the function? can we run a command like this via nativeQuery?
Problem with query in MySql
String sql = "SELECT siteapplications.Application, Count(visits.VisitId) AS CountOfVisitId FROM visits, siteapplications WHERE visits.SiteApplicationId=siteapplications.ApplicationID and Month(visits.VisitTime)=" + month + " and Year(visits.VisitTime)=" + year + "GROUP BY siteapplications.Application ORDER BY CountOfVisitId DESC;"; rs = st.executeQuery(sql); When I run it I get this error in java : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BY Application ORDER BY CountOfVisitId DESC' at line 1 I don't see a error ... Can anyone help ... thx all
" and Year(visits.VisitTime)=" + year + "GROUP BY ... Should be: " and Year(visits.VisitTime)=" + year + " GROUP BY ... Your version is missing a space before GROUP.
while executing this sql statement in java i m getting this error
this is my sql statement in java. //validating for employee no exits in database or not String importTable = getConfig().getImportTable(); String sql = "update " + importTable + " set errMsg = case when errMsg is null or errMsg ='' then '' else errMsg + '<br>' end " + "+ 'Employeeno doesn't exists in the database.(' + employeeno + ')' " + " where employeeno is not null and not exists (select * from uae_empinfo where employee = " + importTable + ".cid)"; executeCommand(sql); this is the error:- org.springframework.dao.DataIntegrityViolationException: StatementCallback; SQL []; Invalid SQL statement or JDBC escape, terminating ''' not found.; nested exception is java.sql.SQLException: Invalid SQL statement or JDBC escape, terminating ''' not found.
Your problem is that you have an embedded single quote here: + "+ 'Employeeno doesn't exists in the database.(' + employeeno + ')' " // -------------------^ So you end up with unbalanced single quotes and invalid SQL. You need to properly escape your text before trying to turn it into SQL.
You need to use PreparedStatement, instead.