using spring jpa.
the repository:
public interface FacultyMemberImpl extends JpaRepository<FacultyMember,Long> {
#Query("SELECT e FROM FacultyMember e WHERE e.name =:name")
List<FacultyMember> getFacultyByName(#Param("name") String name);
#Transactional
#Modifying
#Query("UPDATE FacultyMember e SET e.name=:name, e.department=:department WHERE e.id=:id")
List<FacultyMember> updateFaculty(#Param("id") int id,#Param("name") String name,#Param("department") String department);
}
the error
2019-01-28 09:57:53.027 ERROR 14264 --- [nio-8181-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [UPDATE com.facultyProject.faculty_project.dao.FacultyMember e SET e.name=:name, e.department=:department WHERE e.id=:id]; nested exception is java.lang.IllegalStateException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [UPDATE com.facultyProject.faculty_project.dao.FacultyMember e SET e.name=:name, e.department=:department WHERE e.id=:id]] with root cause
Related
I'm not sure why rollback occurs after the first find query. The first query perfectly works fine, but rollback constantly occurs if I try to do a query again.
This is the Controller.
//....
#GetMapping("/s-id/{s-id}")
public ResponseEntity<DefaultResponseDto<Object>> findManyBySId(HttpServletRequest servletRequest,
#PathVariable(name = "s-id")
String sId) {
// ...
List<Member> members = memberService.findManyBySpotrightId(spotrightId);
// ....
}
//...
This is the Service.
#Transactional(readOnly = true)
public List<Member> findManyBySId(String spotrightId) {
try {
return memberRepository.findBySpotrightIdContainsAndIsDeletedIsFalse(spotrightId);
} catch (RuntimeException e) {
throw new CustomException(SERVER_ERROR);
}
}
This is the Repository.
public interface MemberRepository extends JpaRepository<Member, Long> {
List<Member> findBySIdContainsAndIsDeletedIsFalse(String sId);
}
This is the error message that I get in the console
2023-02-06 19:21:48.470 INFO 62504 --- [nio-8080-exec-9] c.s.s.common.aop.TraceAspect : [START] MemberService | findManyBySpotrightId null | sId = admin
2023-02-06 19:21:48.473 INFO 62504 --- [nio-8080-exec-9] c.s.s.common.aop.TraceAspect : [START] MemberRepository | findBySIdContainsAndIsDeletedIsFalse null
2023-02-06 19:21:48.485 INFO 62504 --- [nio-8080-exec-9] p6spy : #1675678908485 | took 1ms | rollback | connection 8| url jdbc:mysql://localhost:3306/spot?serverTimezone=UTC&characterEncoding=UTF-8
;
2023-02-06 19:21:48.498 ERROR 62504 --- [nio-8080-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [\] did not match expected type [java.lang.String (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [\] did not match expected type [java.lang.String (n/a)]] with root cause
java.lang.IllegalArgumentException: Parameter value [\] did not match expected type [java.lang.String (n/a)]
Why does the rollback occur? And How do I fix this problem?
I found out that it is a bug from Hibernate. I fixed it by including #Param("sId") to parameter like below.
List<Member> findBySIdContainingAndIsDeletedIsFalse(#Param("sId") String sId);
I have a created a spring boot app which is calling a Stored procedure to fetch data using CrudRepository. Below is the implementation of Repo interface:
public interface MyRepository extends CrudRepository<EmpEntity, Long> {
#Query(value = "call myStoredProc(:empID, :empProv);", nativeQuery = true)
Map<String, String> findWithNumber(#Param("empID ") String empID, #Param("empProv ") String empProv);
}
The stored procedure is returning 10 variables and out of those 1 value is returning blank value. Due to this behavior I am getting following error:
2020-06-10 08:02:46.118 ERROR 23724 --- [nio-8050-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.StringIndexOutOfBoundsException: String index out of range: 0] with root cause
java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:658) ~[na:1.8.0_144]
at org.hibernate.type.descriptor.java.CharacterTypeDescriptor.wrap(CharacterTypeDescriptor.java:61) ~[hibernate-core-5.4.9.Final.jar!/:5.4.9.Final]
at org.hibernate.type.descriptor.java.CharacterTypeDescriptor.wrap(CharacterTypeDescriptor.java:16) ~[hibernate-core-5.4.9.Final.jar!/:5.4.9.Final]
at org.hibernate.type.descriptor.sql.VarcharTypeDescriptor$2.doExtract(VarcharTypeDescriptor.java:62) ~[hibernate-core-5.4.9.Final.jar!/:5.4.9.Final]
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47) ~[hibernate-core-5.4.9.Final.jar!/:5.4.9.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257) ~[hibernate-core-5.4.9.Final.jar!/:5.4.9.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253) ~[hibernate-core-5.4.9.Final.jar!/:5.4.9.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:249) ~[hibernate-core-5.4.9.Final.jar!/:5.4.9.Final]
I can fix the issue by putting NULLIF(a.addressType,"") addressType in stored procedure but I want to understand the reason behind the issue and also the possible fix in Spring boot app.
I'm new in java and spring, I need to make a query to two tables with a 1 to N pacient->event relationship.
Repository:
public interface EventJpaRepository extends JpaRepository<Events2, Long> {
#Query("select a.start,a.end, CONCAT(p.a_pat ,' ', p.a_mat ,' ',p.nombre) as title from events2,paciente p where e.rut_num=p.rut_num")
List<Events2> getAllEvents();
}
I got this error:
Error creating bean with name 'eventJpaRepository':
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.calendar.repository.EventJpaRepository.getAllEvents()!
I need design delete API, but I got error. I still not get idea where was wrong.
Repository:
public interface ProductRepository extends JpaRepository<Product, Integer> {
#Modifying
#Query(nativeQuery=true, value = "delete from product where productid = ?1")
void deleteProductByProductID(int productID);
}
Controller:
#DeleteMapping(path = "/delete/{id}")
public #ResponseBody
void deleteProduct(#PathVariable("id")int productID) {
productRepository.deleteProductByProductID(productID);
}
API test error:
{
"timestamp": "2018-04-08T08:46:15.639+0000",
"status": 500,
"error": "Internal Server Error",
"message": "could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet",
"path": "/v1/product/delete/30"
}
IDE error:
2018-04-08 01:36:26.890 WARN 13629 --- [nio-8080-exec-1]
o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState:
S1009 2018-04-08 01:36:26.890 ERROR 13629 --- [nio-8080-exec-1]
o.h.engine.jdbc.spi.SqlExceptionHelper : Can not issue data
manipulation statements with executeQuery(). 2018-04-08 01:36:26.906
ERROR 13629 --- [nio-8080-exec-1]
o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for
servlet [dispatcherServlet] in context with path [] threw exception
[Request processing failed; nested exception is
org.springframework.orm.jpa.JpaSystemException: could not extract
ResultSet; nested exception is
org.hibernate.exception.GenericJDBCException: could not extract
ResultSet] with root cause
I am trying to run a simple insert query in spring boot but I am unable to do so.
Query:
#Modifying
#Query(value = "insert into Local(userId,name,address,pin) VALUES (:userId,:name,:address,:pin)", nativeQuery = true)
List < Local > insertattributes(#Param("userId")String userId,#Param("address") String address ,#Param("name")String name,#Param("pin") String pin);
Controller:
#RequestMapping("insertattributes/{userId}/{name}/{address}/{pin}")
#ResponseBody
public List < Local > insertAttributes(#PathVariable String userId, #PathVariable String name, #PathVariable String address, #PathVariable String pin) {
return localService.insertattributes(userId,name,address,pin);
}
Error:
o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: S1009
o.h.engine.jdbc.spi.SqlExceptionHelper : Can not issue data manipulation statements with executeQuery().
[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet] with root cause
java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
An insert statement doesn't return the inserted record. Since you're expecting a list to be returned executeQuery() is executed by Spring. What you want is the executiong of executeUpdate() which is only executed for void methods.
See also Cannot issue data manipulation statements with executeQuery()