I have to retrieve data from a table in db2 using jpa
after all configuration and mapping
when I try to execute a query using the entity manager I get errors don't know where is the problem exactly.
the message error :Error 500: <openjpa-2.1.1-SNAPSHOT-r422266:1141200 fatal general error> org.apache.openjpa.persistence.PersistenceException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=DB2ADMIN.CATEGORIE, DRIVER=4.8.86 {prepstmnt 85179437 SELECT t0.CODE_CAT, t0.LIBELLE_CAT FROM CATEGORIE t0 } [code=-204, state=42704]SQLCA OUTPUT[Errp=SQLNQ1FC, Errd=-2145779603, 0, 0, 0, -10, 0] DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=DB2ADMIN.CATEGORIE, DRIVER=4.8.86 DB2 SQL Error: SQLCODE=-727, SQLSTATE=56098, SQLERRMC=2;-204;42704;DB2ADMIN.CATEGORIE, DRIVER=4.8.86 DB2 SQL Error: SQLCODE=-727, SQLSTATE=56098, SQLERRMC=2;-204;42704;DB2ADMIN.CATEGORIE, DRIVER=4.8.86 FailedObject: select c from Categorie c [java.lang.String]
I had the same problem and i resolved it by adding Schema in my entity :
#Entity
#Table(name="MyTable", schema="MySchemaName")
public class MyClass implements Serializable {
...
}
From the SQLSTATE messages page, the first error (SQLCODE=-204, SQLSTATE=42704) is "An undefined object or constraint name was detected". The second error (SQLCODE=-727, SQLSTATE=56098) is "An error occurred during implicit rebind, recompile, or revalidation.", which probably stems from the -204.
-204 usually means that either the table name is spelled wrong, or it can't find the table for some reason. I don't see a schema on the SQL generated there (SELECT t0.CODE_CAT, t0.LIBELLE_CAT FROM CATEGORIE t0), so perhaps you need to add that.
Related
I'm developing a backend program with springboot + mybatis + Yugabyte.
It works well in the development environment, but got following errors in online environment :
2021-04-14 13:52:15.123 [http-nio-8080-exec-8] DEBUG com.yoi.config.infrastructure.repository.mapper.ConfigMapper.liteConfigByType - ==> Preparing: select * from "equ_config" where conf_type = ? and conf_df = true order by conf_order desc;
2021-04-14 13:52:15.133 [http-nio-8080-exec-8] DEBUG com.yoi.config.infrastructure.repository.mapper.ConfigMapper.liteConfigByType - ==> Parameters: dept(String)
2021-04-14 13:52:15.459 [http-nio-8080-exec-8] ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. Cause: org.postgresql.util.PSQLException: ERROR: relation "equ_config" does not exist
Position: 16
### The error may exist in URL [jar:file:/srvmgmt/java/target/equip-management-server.jar!/BOOT-INF/classes!/mapper/ConfigMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select * from "equ_config" where conf_type = ? and conf_df = true order by conf_order desc;
### Cause: org.postgresql.util.PSQLException: ERROR: relation "equ_config" does not exist
Position: 16
; bad SQL grammar []; nested exception is org.postgresql.util.PSQLException: ERROR: relation "equ_config" does not exist
Position: 16] with root cause
org.postgresql.util.PSQLException: ERROR: relation "equ_config" does not exist
Position: 16
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2532) ~[postgresql-42.2.14.jar!/:42.2.14]
The Mapper is like following :
<select id="liteConfigByType" resultType="com.yoi.config.infrastructure.repository.po.ConfigPO">
select * from equ_config where conf_type = #{type} and conf_df = true
order by conf_order desc;
</select>
The query sql select * from equ_config where conf_type = 'dept' and conf_df = true order by conf_order desc; works well in the Navicat Gui, and the online application starts up well.
I don't know what goes wrong.
To me this looks like a configuration issue somewhere. Without more information, it's difficult to find out the root cause of the issue. It could be that:
The table was not actually created.
The grants were not set up correctly.
The account may be different in PROD.
May have a different case in the table name, or a prefix, or a suffix.
you name it...
A trick that can help you diagnose the issue can take que form of a query on the information_schema, ran from the application itself, to find out what the application is seeing. I have a few "extra" queries in my MyBatis mappers that look like:
<select id="searchTable" resultType="FoundTableVO">
select table_schema, table_name
from information_schema.tables
where lower(table_name) like '%equ_config%'
</select>
The above query will list all the tables that look like your table, and the schema where they are located. A few queries like this one will shed light on the issue.
Make sure that the table equ_config is in the schema public. If it's not, try this syntax using qweries with your table: "schema_name.equ_config"
Hibernate generating wrong sql(MySQL) query with syntax error.
given this HQL query :
"update GpClientContacter set id = :newKey where id = :Key"
Hibernate executes this sql query:
Hibernate: update GP_CLIENT_CONTACTER set CODE_CLIENT, NOM_RS=(?, ?) where (CODE_CLIENT, NOM_RS)=(?, ?)
Here is exception message:
ERROR: 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 ' NOM_RS=(15, 'test1') where (CODE_CLIENT, NOM_RS)=(15, 'test5')' at line 1
.
.
org.hibernate.exception.SQLGrammarException: could not execute statement
id is an EmbeddedId that has the codeClient and nomRs fields. :key and :newKey are instances of the EmbeddedId Type
Entity Class
#Id
#Column(length=3)
private String BankID;
DAO Class
String sSQL = "SELECT x FROM Bank x WHERE x.BankID =?1 ";
Query aQuery = m_aEntityManager.createQuery(sSQL);
aQuery.setParameter(1, sBankID);
If I pass '001' to sBankID, everything is OK.
But if I pass '0001' to sBankID, OpenJPA just throw PersistenceException: DB2 SQL error: SQLCODE: -302, SQLSTATE: 22001, SQLERRMC: null
Is there anyway just to set OpenJPA turn off column lenth validation?(if I don't want modify any eneity and dao source codes)
DB2 9, OpenJPA 1.2
Thanks a lot!
Is there anyway just to set OpenJPA turn off column lenth
validation?(if I don't want modify any eneity and dao source codes)...
DB2 SQL error: SQLCODE: -302, SQLSTATE: 22001, SQLERRMC: null
This exception is coming from DB2 and OpenJPA is merely exposing the problem as a PersistenceException. You will need to pass in proper length data to get the exception to go away... or modify your table definition.
Hi am trying to do a QueryForInt using spring jbdc.The query returns a simple count.On execution i get springframework.jdbc.UncategorizedSQLException:java.sql.SQLException: Invalid column type.
I did find similar posts and tried the suggestions but i am still stuck:(...
Any help is appreciated.
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue(DAOConstants.PROD_ID, custVo.getProdId(),OracleTypes.NUMBER);
params.addValue(DAOConstants.REQ_IND, DAOConstants.FLAG_Y,OracleTypes.VARCHAR);
The query:
select count(1) from prod where prod_id = :PROD_ID and req_ind =:REQ_IND
Table definition
Name Null Type
----------------- -------- ------------
PROD_ID NOT NULL NUMBER(5)
REQ_IND VARCHAR2(10)
The Logs..
Caused by: org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback;
uncategorized SQLException for SQL [select count(1) from prod where prod_id = :PROD_ID and req_ind = :REQ_IND ]; SQL state [99999]; error code [17004]; Invalid column type; nested exception is java.sql.SQLException: Invalid column type
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:83)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:602)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:636)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:665)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:673)
at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:728)
at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:744)
at org.springframework.jdbc.core.JdbcTemplate.queryForInt(JdbcTemplate.java:775)
... 45 more
Caused by: java.sql.SQLException: Invalid column type
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:263)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:271)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:445)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:7937)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:7517)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:8174)
at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:8155)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.setObject(OraclePreparedStatementWrapper.java:230)
at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.setObject(WrappedPreparedStatement.java:724)
at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:351)
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:216)
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:144)
at org.springframework.jdbc.core.ArgPreparedStatementSetter.doSetValue(ArgPreparedStatementSetter.java:65)
at org.springframework.jdbc.core.ArgPreparedStatementSetter.setValues(ArgPreparedStatementSetter.java:46)
at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:641)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:586)
... 56 more
Based on the stacktrace it looks like you are using the JdbcTemplate class. Since you are using the named parameter style place holders and providing a MapSqlParameterSource for the parameters you need to use the NamedParameterJdbcTemplate.
So, why are you getting this strange error message? Well, your SQL query actually works using the Oracle JDBC driver. It does allow you to use either "?" or a named parameter style using a ":" as the prefix fro the placeholders. You still need to provide the parameter values in the correct order which the MapSqlParameterSource in your case does not. You can test this by using an Object array instead like:
new Object[] {custVo.getProdId(), DAOConstants.FLAG_Y}
The error is based on the values coming in the wrong order from the params.getValues() method, with the String value being passed in for the :PROD_ID.
When I execute the following code
return entityManager
.createQuery("select a from Article where a.slug = ?1", Article.class)
.setParameter(1, slug)
.getSingleResult();
I get the following exception
java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing the query [select a from Article where a.slug = '?1'], line 1, column 22: syntax error at [where].
Internal Exception: MismatchedTokenException(77!=78)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1328)
I'm using JPA 2 with EclipseLink 2.0.2.
What is wrong with my query?
... From Article a ... (missing alias)
Well, the answer has alredy given.. But what I dont like about JPQL, you have to put an identifier after Entity name, but it is uncessary if your from clause does have only one Entity. Most of time, I also forget to put that unnecessarily required identifier.
I wish I would write the above query as below;
select * from Article where slug = ?1