JDBC template throws NullPointerException when no rows found - spring

I have a query to sum numeric values from single column:
int numberOfDays = jdbcTemplate.queryForObject
("select sum(number_of_days) from business_trip where name = ? and surname = ? and type = ?",new Object[]{name,surname,"UE"},Integer.class);
It works fine as long as there is a business trip of UE category for selected user. Then it sums trip days correctly. However, if there are no trips of UE category, instead of zero I'm getting NullPointerException. I can place this line inside try/catch but that would be a workaround. I would like to figure out why NPE is thrown in such case.
*2016-11-24 11:13:02.613 ERROR 8316 --- [nio-8080-exec-2] 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.NullPointerException] with root cause java.lang.NullPointerException: null
at com.tfb.dao.BusinessTripDAO.getKrajoweTripsDaysForEmployee(BusinessTripDAO.java:59) ~[classes/:na]*

Change your select statement to
"select coalesce(sum(number_of_days), 0) from business_trip where name = ? and surname = ? and type = ?"
This way, if you get null it will be transformed to 0.

Related

Getting anonymous error on JoinEnrichment processor org.apache.calcite.sql.validate.SqlValidatorExceptionannot

I am getting error on JoinEnrichment and don't know why it is coming.
My query in JoinEnrichment :
SELECT original.*, enrichment.*
FROM original
FULL JOIN enrichment
ON original.name = enrichment.name
Error
JoinEnrichment[id=a17b1f06-f80c-3641-945f-c2ff331f8028] Failed to join 'original' FlowFile FlowFile[filename=cmdbci-mtaas] and 'enrichment' FlowFile FlowFile[filename=cmdbci-mtaas]; routing to failure: java.sql.SQLException: Error while preparing statement [SELECT original., enrichment.
FROM original
FULL JOIN enrichment
ON original.name = enrichment.name]
Caused by: org.apache.calcite.runtime.CalciteContextException: From line 4, column 4 to line 4, column 34: Cannot apply '=' to arguments of type '<JAVATYPE(CLASS JAVA.LANG.OBJECT)> = <JAVATYPE(CLASS JAVA.LANG.STRING)>'. Supported form(s): '<COMPARABLE_TYPE> = <COMPARABLE_TYPE>'
Caused by: : Corg.apache.calcite.sql.validate.SqlValidatorExceptionannot apply '=' to arguments of type '<JAVATYPE(CLASS JAVA.LANG.OBJECT)> = <JAVATYPE(CLASS JAVA.LANG.STRING)>'. Supported form(s): '<COMPARABLE_TYPE> = <COMPARABLE_TYPE>'
Sample input from original
Location,Environment,ip_address,category,name,dv_sys_updated_on
"",,,Hardware,Ndiggan,2022-12-17 22:37:28
"",,,Hardware,class,2022-12-31 22:37:38
"",,,,Vlan2,2022-12-27 02:17:13
Sample input from enrichment
Location,Environment,ip_address,category,name,dv_sys_updated_on
"",,,Hardware,vpna,2022-12-17 22:36:02
"",,,Hardware,dlcccno,2022-12-17 22:37:04
"",,,Hardware,Ndiggan,2022-12-17 22:37:28

#Cacheable in java 17

I have an application with spring 2.6.8, I have migrated it to 2.7.4.
And everything works correctly.
But when I have upgraded from java 11 to java 17 it gives me the following error
#Cacheable(value = "datoAuxiliar", key = "{#nombre, #tipo}", unless = "#result == null")
public DatoAuxiliar findByTipoAndSecurityDomainAndNombre(TipoDatoAuxiliar tipo, SecurityDomain securityDomain,
String nombre);
-- ERROR
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tipoContratoSelectItemController': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Null key returned for cache operation (maybe you are using named params on classes without debug info?) Builder[public abstract java.util.List com.xxx.xxx.repository.DatoAuxiliarRrhhRepository.findByTipoAndSecurityDomainAndDeletedIsNull(com.abalia.elser.domain.enumeration.TipoDatoAuxiliar,com.abalia.elser.domain.SecurityDomain)] caches=[datoAuxiliar] | key='#tipo' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='#result == null' | sync='false'
I've tried several things but they don't work for me.
Can someone help me with this problem?
Thanks.

kotlin, spring, mybatis cannot execute foreach

I tried to execute this sql but Exception happened.
source code is here
#Select(
"""
SELECT
COUNT(*)
FROM
order
WHERE
order_id IN
<foreach item="id" collection="orderIds" open="(" separator="," close=")" >
#{id}
</foreach>
"""
)
fun findByOrderIds(#Param(value = "orderIds") orderIds: List<String>): Int
Error message is here↓
nested exception is org.apache.ibatis.binding.BindingException: Parameter 'id' not found. Available parameters are [orderIds, param1]
I cannot find wrong point.
If you have idea please let me know.

I am getting exception on a post request logged in stackify but not able to find cause of the error

Following is the exception stackify shows me on dashboard:
ERROR The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.
System.FormatException: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0. ---> System.FormatException: null is not a valid value for DateTime.
at System.DateTimeParse.Parse
at System.ComponentModel.DateTimeConverter.ConvertFrom
--- End of inner exception stack trace ---
at System.ComponentModel.DateTimeConverter.ConvertFrom
at System.ComponentModel.NullableConverter.ConvertFrom
at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.SimpleTypeModelBinder.BindModelAsync
I am not able to understand which field of the viewmodel causes the issue like this. How can I catch this exception on my local code?

Entity Manager EJB query could not locate named parameter

enter code hereHi I'm using Entity manager(EJB) which internally depends on hibernate to get Object.
My query is ........
TypedQuery<FraudDetectionInfoEntity> queryKount = em_oltp.createQuery("SELECT o FROM FraudDetectionInfoEntity o WHERE o.order.orderId=:orderId",FraudDetectionInfoEntity.class);
query.setParameter("orderId", orderEntity.getOrderId());
FraudDetectionInfoEntity infoEntity=queryKount.getSingleResult();
I'm getting folowing error If I run that query.
org.hibernate.QueryParameterException: could not locate named parameter [orderId]
org.hibernate.QueryParameterException: could not locate named parameter [orderId]
12:46:58,639 ERROR [com.vip.enterprise.admin.web.controller.OrderPlacementController] (http-/127.0.0.1:8084-6) error com.vip.enterprise.service.exception.ServiceException: org.hibernate.QueryParameterException: could not locate named parameter [orderId]
12:47:00,001 ERROR [com.vip.enterprise.service.jobs.ReplenishmentJob] (EJB default - 4) Replenish Orders JOB : Mon Jan 27 12:47:00 IST 2014
Your code is ok. Missing variable query or queryKount?
TypedQuery<FraudDetectionInfoEntity> queryKount = em_oltp.createQuery("SELECT o FROM FraudDetectionInfoEntity o WHERE o.order.orderId=:orderId",FraudDetectionInfoEntity.class);
query.setParameter("orderId", orderEntity.getOrderId());
<or>
queryKount.setParameter("orderId", orderEntity.getOrderId());

Resources