vladmihalcea / hypersistence-optimizer

Hypersistence Optimizer allows you to get the most out of JPA and Hibernate. By scanning your application configuration and mappings, Hypersistence Optimizer can tell you what changes you need to do to speed up your data access layer.
https://vladmihalcea.com/hypersistence-optimizer/
Apache License 2.0
318 stars 43 forks source link

NullPointerException thrown by the isQueryFilteringByEntityId method #176

Closed pkernevez closed 2 years ago

pkernevez commented 2 years ago

Version 2.6.1

java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because the return value of "java.util.Map.get(Object)" is null
    at io.hypersistence.optimizer.hibernate.decorator.AbstractHibernate56SessionFactoryDecorator$SessionDecorator$QueryDecorator.isQueryFilteringByEntityId(AbstractHibernate56SessionFactoryDecorator.java:1407)
    at io.hypersistence.optimizer.hibernate.decorator.AbstractHibernate56SessionFactoryDecorator$SessionDecorator$QueryDecorator.checkOrderByClause(AbstractHibernate56SessionFactoryDecorator.java:1371)
    at io.hypersistence.optimizer.hibernate.decorator.AbstractHibernate56SessionFactoryDecorator$SessionDecorator$QueryDecorator.access$2200(AbstractHibernate56SessionFactoryDecorator.java:1191)
    at io.hypersistence.optimizer.hibernate.decorator.AbstractHibernate56SessionFactoryDecorator$SessionDecorator$QueryDecorator$1.execute(AbstractHibernate56SessionFactoryDecorator.java:1322)
    at io.hypersistence.optimizer.core.action.ActionExecutor.executeThrough(ActionExecutor.java:81)
    at io.hypersistence.optimizer.hibernate.decorator.AbstractHibernate56SessionFactoryDecorator$SessionDecorator$QueryDecorator.checkBeforeQuery(AbstractHibernate56SessionFactoryDecorator.java:1319)
    at io.hypersistence.optimizer.hibernate.decorator.AbstractHibernate56SessionFactoryDecorator$SessionDecorator$QueryDecorator.getSingleResult(AbstractHibernate56SessionFactoryDecorator.java:1271)
    at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.getSingleResult(CriteriaQueryTypeQueryAdapter.java:111)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:198)

This happen when calling: `repository.findFirstByFxRateBaseCurrencyAndFxRateQuoteCurrencyAndDateLessThanEqualOrderByDateDesc(CurrencyEntity1, CurrencyEntity2, LocalDate) I don't found the source code for this class, so I can't do investigation.

vladmihalcea commented 2 years ago

Thanks for reporting the issue. Would you mind providing the signature for that findFirstByFxRateBaseCurrencyAndFxRateQuoteCurrencyAndDateLessThanEqualOrderByDateDesc method?

I want to see what it returns so that I can figure out the problem.

pkernevez commented 2 years ago

Yes it's:

   Optional<CurrencyExchangeRateHistoryEntity> findFirstByFxRateBaseCurrencyAndFxRateQuoteCurrencyAndDateLessThanEqualOrderByDateDesc(
            CurrencyEntity baseCurrency,
            CurrencyEntity quoteCurrency,
            LocalDate date);
vladmihalcea commented 2 years ago

Thanks. I'll try to replicate it.

vladmihalcea commented 2 years ago

@pkernevez I have a few questions.

  1. Is the CurrencyExchangeRateHistoryEntity mapped as an entity, right?
  2. Are these properties mapped as regular entity attributes: fxRateBaseCurrency, fxRateQuoteCurrency, and date?

Or are they derived using @Formula or something like that?

Can you add the entity mapping for the CurrencyExchangeRateHistoryEntity as I can't figure out why one of the properties could not be found in the EntityPersister while it was sued by the query?

pkernevez commented 2 years ago

I've question about hibernate behavior and I'm preparing a simple project for the forum, I will try to reproduce this behavior in it then share it here.

vladmihalcea commented 2 years ago

Thanks. In the meantime, I'll add some extra checks for this as I could replicate something similar with a composite id.

s-jepsen commented 2 years ago

Got a similar issue.

vladmihalcea commented 2 years ago

@s-jepsen The fix is in the making. I'll release a new patch version next week.

pkernevez commented 2 years ago

Do you still need additional information ?

vladmihalcea commented 2 years ago

@pkernevez It's fine at the moment. I'll let you know when the release is ready so that you can test it. I'm now working on fixing it for all verisons.

vladmihalcea commented 2 years ago

Fixed.