#Query(value = "DECLARE #StartDateTime DATETIME = DATEADD(hour,-1,GETUTCDATE()) EXEC [Fetch].[mevisio_downtimedata] " +
"#StartDateTime = :startDateTime," +
"#workCenterList = :workCenterList",nativeQuery = true)
List<DownTimeData> retriveOneHrDowntime(#Param("startDateTime") LocalDateTime startDateTime, #Param("workCenterList") List<String> workCenterList);
Getting Error:
2021-05-06 00:26:04.506 ERROR 14840 --- [ scheduling-1] o.h.engine.jdbc.spi.SqlExceptionHelper : Incorrect syntax near '('.
2021-05-06 00:26:04.513 ERROR 14840 --- [ scheduling-1] o.s.s.s.TaskUtils$LoggingErrorHandler : Unexpected error occurred in scheduled task
org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
You cannot execute stored procedures with a #Query
You have to define a NamedStoredProcedureQuery on an entity
#Entity
#NamedStoredProcedureQuery(name = "User.plus1", procedureName = "plus1inout", parameters = {
#StoredProcedureParameter(mode = ParameterMode.IN, name = "arg", type = Integer.class),
#StoredProcedureParameter(mode = ParameterMode.OUT, name = "res", type = Integer.class) })
public class User {}
And then you can use #Procedure to execute it
#Procedure
Integer plus1inout(#Param("arg") Integer arg);
Please read the documentation:
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.stored-procedures
Related
I'm trying to create a page object from a repository query, when return the data to a List works fine, but if i try to return the data to a Page object the query fails, getting this error
[nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [17]
2023-01-05 12:34:09.327 WARN 20644 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 42601
2023-01-05 12:34:09.331 ERROR 20644 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: syntax error at or near "WHERE"
What am i doing wrong?
LIST WAY (WORKS FINE)
Query
#Query(value ="SELECT field1 as idSolicitud, field2 as numeroRadicado, "
+ "field3 as tipoTramite, field4 as nombreTramite "
+ "FROM myTable WHERE field5 = :paramet", nativeQuery = true)
List<SolicitudesInterfaceDTO> buscarSolicitudes(#Param("paramet") Long paramet);
DTO (Interface)
public interface SolicitudesInterfaceDTO {
Long getidSolicitud();
String getnumeroRadicado();
String gettipoTramite();
String getnombreTramite();
String getestado();
}
Execute query
List<SolicitudesInterfaceDTO> solicitudesUsuario = solicitudesRepository.buscarSolicitudes(idUsuario);
PAGE WAY (Doesn't work)
Query
#Query(value ="SELECT field1 as idSolicitud, field2 as numeroRadicado, "
+ "field3 as tipoTramite, field4 as nombreTramite "
+ "FROM myTable WHERE field5 = :paramet", nativeQuery = true)
Page<SolicitudesInterfaceDTO> buscarSolicitudesPaginado(#Param("paramet") Long paramet, Pageable pageable);
Execute query
Page<SolicitudesInterfaceDTO> paginaSolicitudesUsuario = solicitudesRepository.buscarSolicitudesPaginado(idUsuario, pageable);
I have this exception with mybatis
--- Cause: java.lang.ClassCastException: oracle.jdbc.driver.OracleDatabaseMetaData cannot be cast to oracle.jdbc.OracleDatabaseMetaData; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in sqlMaps/WorkSession.xml.
--- The error occurred while applying a result map.
--- Check the getChangeSeller-AutoResultMap.
--- Check the result mapping for the 'IS_RESTRICTED' property.
--- Cause: java.lang.ClassCastException: oracle.jdbc.driver.OracleDatabaseMetaData cannot be cast to oracle.jdbc.OracleDatabaseMetaData
And this is my sqlMap workSession.getChangeSeller :
<select id="getChangeSeller" parameterMap="getChangeSellerParam" resultClass="Boolean">
select decode(count(*),0,0,1) is_restricted
from users u inner join user_sdp us on u.user_id = us.user_id
inner join sdp s on s.sdp_id = us.sdp_id
where u.user_id = ? and s.sdp_id = ? and s.self_cash_cycle_only = 1 and u.self_cash_cycle_only =1
</select>
I want to update mysql table from a dto list, but there is some exception, something like grammer issue, here is my code:
#Override
#Transactional
public void updateCustomCategory(List<ItemDto> itemDtoList) {
if (CollectionUtils.isNotEmpty(itemDtoList)) {
for (ItemDto itemDto : itemDtoList) {
Long l1CustomCategoryId = itemDto.getL1CustomCatId();
Long l2CustomCategoyId = itemDto.getL2CustomCatId();
StringBuilder query =
new StringBuilder(
"update ItemDto item set item.l1CustomCategoryId = :l1CustomCategoryId and item.l2CustomCategoryId = :l2CustomCategoyId where itemId = :itemId");
Query q = this.em.createQuery(query.toString());
q.setParameter("l1CustomCategoryId", l1CustomCategoryId);
q.setParameter("l2CustomCategoyId", l2CustomCategoyId);
q.setParameter("itemId", itemDto.getItemId());
q.executeUpdate();
}
}
}
and exception as below:
2019-10-04 14:49:00.227 ERROR 11909 --- [nio-9092-exec-5] 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.ast.QuerySyntaxException: unexpected token: and near line 1, column 108 [update com.shopee.data.dto.brandseller.item.ItemDto item set item.l1CustomCategoryId = :l1CustomCategoryId and item.l2CustomCategoryId = :l2CustomCategoyId where itemId = :itemId]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: and near line 1, column 108 [update com.shopee.data.dto.brandseller.item.ItemDto item set item.l1CustomCategoryId = :l1CustomCategoryId and item.l2CustomCategoryId = :l2CustomCategoyId where itemId = :itemId]] with root cause
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: and near line 1, column 108 [update com.shopee.data.dto.brandseller.item.ItemDto item set item.l1CustomCategoryId = :l1CustomCategoryId and item.l2CustomCategoryId = :l2CustomCategoyId where itemId = :itemId]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
at org.hibernate.hql.internal.ast.ErrorTracker.throwQueryException(ErrorTracker.java:93) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:296) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:188) ~[hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile
You don't use AND with SET, you separate them with commas. The correct syntax is
SET item.l1CustomCategoryId = :l1CustomCategoryId, item.l2CustomCategoryId = :l2CustomCategoyId
Good morning, I'm dwelling with this error on a DAOImpl method from this morning... it happened all of a sudden..
This is my method:
public int postiOccupatiParcheggio(Park park) {
Query pianiQuery = getSession().createQuery("from Piano p where p.park = :park");
pianiQuery.setParameter("park", park);
List <Piano> piani = pianiQuery.list();
Query postiQuery = getSession().createQuery("from Posto p where p.piano in :piani");
postiQuery.setParameterList("piani", piani);
List<Posto> posti = postiQuery.list();
Query query = getSession().createQuery("SELECT count(*) from Occupazione o where o.posto in :posti "
+ "and o.occupied = true and o.dateTime = (select max(oo.dateTime) from "
+ "Occupazione oo where o.posto = oo.posto)");
query.setParameterList("posti", posti);
Long r = null;
int result = 0;
try {
r = (Long) query.uniqueResult();
result = r.intValue();
} catch(NoResultException nre) {
result = 0;
}
return result;
}
I'm getting this exception
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 ')) and occupazion0_.isOccupied=1 and
occupazion0_.date_time=(select max(occupazi' at line 1
and hibernate logs tells:
SQL Error: 1064, SQLState: 42000
I read that this is an error that occurs if you use some restricted variables names, but it's not the case, and most of all, everything went good till yesterday...
I'm using MySql 5.7.9 and with this property:
hibernate.dialect = org.hibernate.dialect.MySQLDialect
This error is thrown from one of my jersey-glassfish rest endpoint ( from domain logs ):
Caused by: java.lang.IllegalArgumentException: Type specified for TypedQuery [com.tanukis.streetama.entity.Flow] is incompatible with query return type [interface java.util.Set]
at org.hibernate.ejb.AbstractEntityManagerImpl.createNamedQuery(AbstractEntityManagerImpl.java:458)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createNamedQuery(EntityManagerWrapper.java:566)
at com.tanukis.streetama.dao.FluxManager.getBlacklist(FluxManager.java:571)
The query is defined in orm.xml :
SELECT DISTINCT s.blacklistedFlow FROM StreetamaUser s WHERE s.uid = :uid
Here is my StreetamaUser entity:
#ManyToMany(cascade= javax.persistence.CascadeType.ALL)
#JoinTable(
name="ws_user_blacklist",
uniqueConstraints = #UniqueConstraint(columnNames = { "blacklisted_flow_id", "user_id" }),
joinColumns = {
#JoinColumn(name="user_id",referencedColumnName="uid")
},
inverseJoinColumns = {
#JoinColumn(name="blacklisted_flow_id",referencedColumnName="id")
}
)
#XmlTransient
private Set<Flow> blacklistedFlow = new HashSet<Flow>();
And the query call:
List<Flow> result = em.createNamedQuery( "StreetamaUser.findBlacklist", Flow.class )
.setParameter("iduser", uid )
.setFirstResult(startitem)
.setMaxResults(itemnbr)
.getResultList();
I can't understand the Hibernate exception. getResultList return a list, so why it complains about the query return type ?
You can try to use a java.Util.List instead of a Set.
You don't. It is always a List even if it doesn't contains duplicate values like set. I don't see why it matters in the first place.