Still new to hibernate custom queries.
My table is
ID TID R1 Position
1 1 1 2
2 1 1 3
I want a custom query to delete rows with TID 1 and R1 1
My current sql looks like
#Query(value = "delete from Table t where t.TID= :tid and t.R1 = :r1", nativeQuery = true)
void deleteByTIDAndR1(#Param("tid") Integer TID, #Param("r1") Integer r1);
It gives me the following error:
.springframework.orm.jpa.JpaSystemException: could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet] with root cause
My controller retrieves the right tid and r1 ID's.
Is it even possible to delete multiple rows at once? And where could my error be?
Edit
After I add #Modyfying to the query i get the error
TransactionRequiredException: Executing an update/delete query
And after i add #Transactional in combination with #Modifying i get
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 't where t.tid= 1 and t.R1 = 99' at line 1
I think you lack the #Modifying annotation, indicating a query that modify the database:
#Modifying
#Query(value = "delete from Table where TID= :tid and R1 = :r1", nativeQuery = true)
void deleteByTIDAndR1(#Param("tid") Integer TID, #Param("r1") Integer r1);
And yes this method can delete zero, one or more rows.
Related
I am getting error in below query as
#Query(value = "SELECT top 1 * from Job where status = 'Success' order by 1 desc")
public Job getLastJob();
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: top near line 1,
I want to select 1st row which has success status order by id desc
JPA repository throwing Error for custom query method:
org.h2.jdbc.JdbcSQLException: Table "NBMRBANKDTLSENTITY" not found; SQL statement:
select NBMRBankDtlsEntity from NBMRBankDtlsEntity where NBMRBankDtlsEntity.ipphId = ? [42102-191]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
Class :
#Repository
public interface NBMRBankDtlsRepository extends JpaRepository<NBMRBankDtlsEntity, Long> {
#Query(value ="select n from NBMRBankDtlsEntity n where n.ipphId = :ipphId",nativeQuery = true)
Optional<NBMRBankDtlsEntity> findByIPPHId(#Param("ipphId") Long ipphId);
}
The error message tells you: Table "NBMRBANKDTLSENTITY" not found. Therefore it probably doesn't exist. To fix this you'll have to create the table, manually through a script or through hibernates schema creation feature.
I am already creating a table also inserting the record, After that only i am calling this custom query method.
I have found the issue as i am using nativeQuery = true so it is expecting normal sql query to query DB directly not the java query which is creating issue. Now after changing below it works fine ,
#Query(value = "SELECT * from NB_MR_BANK_DTLS WHERE IPPH_ID = :ipphId",nativeQuery = true)
For java query we can use directly as it internally converts to SQL while querying the DB,
#Query(value = "select p from NBMRBankDtlsEntity p WHERE p.ipphId = :ipphId")
I am using Spring JdbcDaoSupport in my DAO and trying to update the records using the following query.
String callersUpdateQuery = "update W67U999S a set pcrdattim= ? where exists (select b.CRDATTIM, b.RECORDCD, b.CRNODE, b.UNITCD, b.WRKTYPE from W03U999S b where a.PCRDATTIM = ? and a.CCRDATTIM = b.CRDATTIM and a.CRECORDCD = b.RECORDCD and a.CCRNODE = b.CRNODE and a.PRECORDCD = 'F' and a.PCRNODE = '01' and b.WRKTYPE = 'CALLER' and b.UNITCD=? and a.crecordcd='T')";
The following is the code that should update the table:
int updatedRowsCount =getJdbcTemplate().update(callersUpdateQuery, new Object[]{newFolderCrdattim, crdattim, businessAreaName});
But getJdbcTemplate().update() is not updating the required rows and returning the updated rows count as zero. Weirdly, the same sql query when I execute at the database end, the records are getting updated. Can anyone guess what's wrong with the code or query?
The log messages are also showing correct values, but somehow the query is not updating the database:
21:04:01,288 DEBUG [org.springframework.jdbc.core.JdbcTemplate.execute] Executing prepared SQL statement [update W67U999S a set pcrdattim= ? where exists (select b.CRDATTIM, b.RECORDCD, b.CRNODE, b.UNITCD, b.WRKTYPE from W03U999S b where a.PCRDATTIM = ? and a.CCRDATTIM = b.CRDATTIM and a.CRECORDCD = b.RECORDCD and a.CCRNODE = b.CRNODE and a.PRECORDCD = 'F' and a.PCRNODE = '01' and b.WRKTYPE = 'CALLER' and b.UNITCD=? and a.crecordcd='T')]
21:04:01,288 DEBUG [org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection] Fetching JDBC Connection from DataSource
21:04:01,288 DEBUG [org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver] Creating new JDBC DriverManager Connection to [jdbc:oracle:thin:#10.193.244.225:1521:AWD]
21:04:03,865 TRACE [org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal] Setting SQL statement parameter value: column index 1, parameter value [2017-08-09-10.33.10.168480], value class [java.lang.String], SQL type unknown
21:04:03,865 TRACE [org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal] Setting SQL statement parameter value: column index 2, parameter value [2017-07-20-04.22.20.893340], value class [java.lang.String], SQL type unknown
21:04:03,865 TRACE [org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal] Setting SQL statement parameter value: column index 3, parameter value [CS2XAA], value class [java.lang.String], SQL type unknown
21:04:04,115 DEBUG [org.springframework.jdbc.core.JdbcTemplate.doInPreparedStatement] SQL update affected 0 rows
21:04:04,131 DEBUG [org.springframework.jdbc.datasource.DataSourceUtils.doReleaseConnection] Returning JDBC Connection to DataSource
Oracle CHAR type is the culprit here. The columns that I want to update are of type CHAR. That's causing the issue. This link helped me in figuring out the solution:
Oracle JDBC and Oracle CHAR data type
i am trying to fetch the id of last column in descending order.
the query which returns last column is
select id from(select id from challan
order by id desc) where ROWNUM=1;
now i am trying to do same thing using hibernate.
public long getIdOnChallanTable() {
session = sessionFactory.openSession();
trans = session.beginTransaction();
Query<Object[]> query = session.createNativeQuery("select id
from(select id from challan order by id desc) where ROWNUM=1;");
Long value = 0L;
List<Object[]> list = query.getResultList();
for ( Object lst : list){
Object[] objects =(Object[]) lst;
value=(Long)(objects[0]);
}
return value;
}
and the error is:
2017-07-26 12:37:36 [http-nio-7080-exec-1] WARN :: SQL Error: 911, SQLState: 22019
2017-07-26 12:37:36 [http-nio-7080-exec-1] ERROR:: ORA-00911: invalid character
update error javax.persistence.PersistenceException:
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
You don't need the semicolon at the end of the query and please use proper whitespacing. In the FROM clause, you don't have the whitespace between the subquery and the FROM keyword.
Note: don't forget to commit/rollback the transaction at the end and handle the exceptions as well. I hope this was just a sketch to show us the problem and not a code from a real world application.
I am getting an exception on executing below MySQL query
hibernate dialect used is org.hibernate.dialect.MySQLDialect
following is my simple query.
String queryString= "select action,user_role ,action_desc , action_timestamp, action_done from event_details Where (general_type =1 or disco_type =1 or mask_type =1) and hadoop_type =0 AND COALESCE(source,'Structured') IN ('Both','Structured') and COALESCE(user_id,-1) =1 and Date(action_timestamp) between '2014-01-09' and '2014-04-09' ";
Query query = session.createSQLQuery(queryString);
List list= query = query.list(); ...... This line throws exception.
when same query is executed for count i.e select count(*) from (queryString ) t1;
Then it gets executed fine.
Please suggest what could be the problem ? and how can solve it ??
Problem resolved after adding Scalars to the query.