Home > OS >  Unable to locate Attribute with the the given name [ANumber] on this ManagedType [com.company.domain
Unable to locate Attribute with the the given name [ANumber] on this ManagedType [com.company.domain

Time:12-06

I just upgraded my project to spring5 which is causing the below error for the code which was working fine with earlier spring version.

The problem is JPA should map aNumber to ANumber but its not doing so its taking ANumber as ANumber rather than aNumber which is causing this issue.

I see one thread for spring team but cannot see the resolution here: https://github.com/spring-projects/spring-data-jpa/issues/1247

I tried to search everywhere but didn't find anything useful.

Domain Class:

@Data
@Entity
public class Statistics {

    @Id
    private Long id;

    private String aNumber;
    private Integer dispositionCode;

    @CreatedDate
    private OffsetDateTime created;

}

the method in Repository interface:

Optional<Statistics> findFirstByIdAndANumberAndDispositionCodeInOrderByCreatedDesc(Long id, String aNumber, Integer... dispositionCode);

Note: i know id can return unique result just to make exact example I made that change.

CodePudding user response:

I was facing this problem after the JPA version upgrade which state that all the names were correct and mapped perfectly.

The solution I found was defining the native query within the @Query annotation of the spring data JPA because the problem was occurring when JPA was trying to create the JPA Named Query.

@Query(nativeQuery = true, value = "Your Query here")
Optional<Statistics> findFirstByIdAndANumberAndDispositionCodeInOrderByCreatedDesc(Long id, String aNumber, Integer... dispositionCode);

This might not be the best solution but in my case I was not supposed to change the version as it was coming from the parent POM and this was the easiest solution which I could do.

  • Related