0

I have this JPA Repository for a user model. And I want to take the very recent data which is the first data that came out. But since I'm using spring-data-jpa.1.0.2-REALEASE, I can't use "findFirstBy" to get what I want.

so I try using nativequery, which has a lot of ways to use and is different in every version. (I guess) anyway, my code would be something like this :


public interface UserRepository extends JpaRepository<UserModel, Long>, QueryDslPredicateExecutor<UserModel> {

    @Query("SELECT u FROM user u where u.userDevice_id = :uid ORDER BY u.Time DESC LIMIT 1")
    UserModel findFirstByUserDeviceOrderByTimeDesc(@Param("uid") Long uid);
    
.......
    
}

but it returning this error

org.hibernate.hql.ast.QuerySyntaxException: user is not mapped

I can't carelessly change my spring version because I'm using an old environment.

  • This might help you out: https://stackoverflow.com/questions/23018836/org-hibernate-hql-internal-ast-querysyntaxexception-table-is-not-mapped – Stultuske Nov 10 '22 at 07:17

1 Answers1

0

Normally if you want to use a native query you have to tell Spring Data JPA by using

@Query(... nativeQuery=true)

But version 1.0.2 doesn't have that attribute. I suspect it didn't support native queries back then.

But your query looks like mixup between SQL (i.e. native) and JPQL/HQL anyway and you should be able to fix that.

The following should work

SELECT u 
FROM User u where u.userDevice.id = :uid ORDER BY u.time DESC LIMIT 1

This assumes the entity class is named User has properties userDevice and time and a user device has an id properties. For all these names the Java class is relevant, not the table or column names.

One final note on

I can't carelessly change my spring version because I'm using an old environment.

I know it is probably not your decision to make, but working with such an old version is not only careless but reckless. It wastes money by having to deal with bugs and missing features which have long be fixed. Including security fixes.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348