micronaut-projects / micronaut-data

Ahead of Time Data Repositories
Apache License 2.0
466 stars 197 forks source link

problem with UUID primary key in findById repository method #2879

Closed alapierre closed 2 months ago

alapierre commented 6 months ago

Expected Behavior

findById repository method not works when key is UUID - it works couple versions ago. In my case it is composit key

repository

@Repository
public interface OperationLogRepository  extends CrudRepository<OperationLog, UUIDTenantId> {
    List<OperationLog> findByTenantAndCreatedBetween(String tenant, LocalDateTime from, LocalDateTime to);
    Optional<OperationLog> findById(UUID id, String tenant);
    Long countByTenant(String tenant);
    Long countByTenantAndNip(String tenant, String nip);
}

OperationLog entity:

@Data
@Entity
@IdClass(UUIDTenantId.class)
@Builder
public class OperationLog {

    @Tolerate
    public OperationLog() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    @JdbcTypeCode(Types.CHAR)
    private UUID id;

    @Id
    private String tenant;

Actual Behaviour

No bean introspection available for type [class java.util.UUID]. Ensure the class is annotated with io.micronaut.core.annotation.Introspected
2024-04-13T09:35:37.643992093Z  at io.micronaut.core.beans.BeanIntrospector.lambda$getIntrospection$3(BeanIntrospector.java:116)
2024-04-13T09:35:37.644000483Z  at java.base/java.util.Optional.orElseThrow(Optional.java:403)
2024-04-13T09:35:37.644008274Z  at io.micronaut.core.beans.BeanIntrospector.getIntrospection(BeanIntrospector.java:116)
2024-04-13T09:35:37.644017868Z  at io.micronaut.core.beans.BeanIntrospection.getIntrospection(BeanIntrospection.java:282)
2024-04-13T09:35:37.644025590Z  at io.micronaut.core.beans.BeanWrapper.getWrapper(BeanWrapper.java:207)
2024-04-13T09:35:37.644033514Z  at io.micronaut.data.runtime.operations.internal.query.DefaultBindableParametersStoredQuery.resolveParameterValue(DefaultBindableParametersStoredQuery.java:216)
2024-04-13T09:35:37.644042101Z  at io.micronaut.data.runtime.operations.internal.query.DefaultBindableParametersStoredQuery.bindParameter(DefaultBindableParametersStoredQuery.java:102)
2024-04-13T09:35:37.644050338Z  at io.micronaut.data.runtime.operations.internal.query.DefaultBindableParametersStoredQuery.bindParameters(DefaultBindableParametersStoredQuery.java:85)
2024-04-13T09:35:37.644058380Z  at io.micronaut.data.runtime.operations.internal.query.DefaultBindableParametersPreparedQuery.bindParameters(DefaultBindableParametersPreparedQuery.java:84)
2024-04-13T09:35:37.644185751Z  at io.micronaut.data.runtime.operations.internal.query.BindableParametersPreparedQuery.bindParameters(BindableParametersPreparedQuery.java:51)
2024-04-13T09:35:37.644264869Z  at io.micronaut.data.runtime.operations.internal.query.DefaultBindableParametersPreparedQuery.bindParameters(DefaultBindableParametersPreparedQuery.java:99)
2024-04-13T09:35:37.644323343Z  at io.micronaut.data.hibernate.operations.AbstractHibernateOperations.bindParameters(AbstractHibernateOperations.java:420)
2024-04-13T09:35:37.644389039Z  at io.micronaut.data.hibernate.operations.AbstractHibernateOperations.bindPreparedQuery(AbstractHibernateOperations.java:488)
2024-04-13T09:35:37.644432717Z  at io.micronaut.data.hibernate.operations.AbstractHibernateOperations.collectResults(AbstractHibernateOperations.java:404)
2024-04-13T09:35:37.644492902Z  at io.micronaut.data.hibernate.operations.AbstractHibernateOperations.collectFindOne(AbstractHibernateOperations.java:325)
2024-04-13T09:35:37.644560596Z  at io.micronaut.data.hibernate.operations.HibernateJpaOperations.lambda$findOne$3(HibernateJpaOperations.java:261)
2024-04-13T09:35:37.644587965Z  at io.micronaut.data.hibernate.operations.HibernateJpaOperations.lambda$executeRead$21(HibernateJpaOperations.java:548)
2024-04-13T09:35:37.644599985Z  at io.micronaut.transaction.support.AbstractPropagatedStatusTransactionOperations.lambda$execute$2(AbstractPropagatedStatusTransactionOperations.java:68)
2024-04-13T09:35:37.644611156Z  at io.micronaut.transaction.TransactionCallback.apply(TransactionCallback.java:37)
2024-04-13T09:35:37.644621525Z  at io.micronaut.transaction.support.AbstractTransactionOperations.executeTransactional(AbstractTransactionOperations.java:333)
2024-04-13T09:35:37.644633489Z  at io.micronaut.transaction.support.AbstractTransactionOperations.executeWithNewTransaction(AbstractTransactionOperations.java:318)
2024-04-13T09:35:37.644645296Z  at io.micronaut.transaction.support.AbstractTransactionOperations.executeNew(AbstractTransactionOperations.java:235)
2024-04-13T09:35:37.644657925Z  at io.micronaut.transaction.support.AbstractTransactionOperations.doExecute(AbstractTransactionOperations.java:137)
2024-04-13T09:35:37.644669845Z  at io.micronaut.transaction.support.AbstractTransactionOperations.lambda$doExecute$0(AbstractTransactionOperations.java:122)
2024-04-13T09:35:37.644683438Z  at io.micronaut.data.connection.support.AbstractConnectionOperations.executeWithNewConnection(AbstractConnectionOperations.java:143)
2024-04-13T09:35:37.644691910Z  at io.micronaut.data.connection.support.AbstractConnectionOperations.execute(AbstractConnectionOperations.java:90)
2024-04-13T09:35:37.644700177Z  at io.micronaut.transaction.support.AbstractTransactionOperations.doExecute(AbstractTransactionOperations.java:120)
2024-04-13T09:35:37.644708223Z  at io.micronaut.transaction.support.AbstractPropagatedStatusTransactionOperations.execute(AbstractPropagatedStatusTransactionOperations.java:65)
2024-04-13T09:35:37.644716421Z  at io.micronaut.transaction.TransactionOperations.executeRead(TransactionOperations.java:76)
2024-04-13T09:35:37.644726253Z  at io.micronaut.data.hibernate.operations.HibernateJpaOperations.executeRead(HibernateJpaOperations.java:548)
2024-04-13T09:35:37.644734362Z  at io.micronaut.data.hibernate.operations.HibernateJpaOperations.findOne(HibernateJpaOperations.java:256)
2024-04-13T09:35:37.644742597Z  at io.micronaut.data.runtime.intercept.DefaultFindOptionalInterceptor.intercept(DefaultFindOptionalInterceptor.java:47)
2024-04-13T09:35:37.644750829Z  at io.micronaut.data.runtime.intercept.DefaultFindOptionalInterceptor.intercept(DefaultFindOptionalInterceptor.java:34)
2024-04-13T09:35:37.644758773Z  at io.micronaut.data.runtime.intercept.DataIntroductionAdvice.intercept(DataIntroductionAdvice.java:83)
2024-04-13T09:35:37.644766829Z  at io.micronaut.aop.chain.MethodInterceptorChain.proceed(MethodInterceptorChain.java:138)
2024-04-13T09:35:37.644774608Z  at io.alapierre.ntt.ksef.integrator.repository.OperationLogRepository$Intercepted.findById(Unknown Source)

Steps To Reproduce

No response

Environment Information

Example Application

No response

Version

4.6.2

radovanradic commented 6 months ago

I checked this in micronaut-data 4.1.4 where it worked without errors (the error begins with 4.2.0). However, even then the query that was being executed was not correct. This is what was being executed SELECT operationLog_ FROM com.example.OperationLog AS operationLog_ WHERE (operationLog_.id = :p1) and because the field is UUID it was returning correct value as being unique. However, I think the correct way would be to remove this custom method Optional<OperationLog> findById(UUID id, String tenant); and use default one Optional<OperationLog> findById(UUIDTenantId id).

alapierre commented 6 months ago

Good point Radovan, thank you. Using default repository method Optional<OperationLog> findById(UUIDTenantId id) connacted with @Introspected annotation works fine.

radovanradic commented 2 months ago

I think this works as expected.