Crava
Crava

Reputation: 76

Using Mysql function in java spring boot

Got a little problem with custom query in my repository:

@Query("select v from Visit v where YEARWEEK('date') = YEARWEEK(curdate())")

Error shows before YEARWEEK('date') opening bracket:

<expression>, <operator>, AND, GROUP, HAVING, IS, OR or ORDER expected, got '('

Upvotes: 0

Views: 1456

Answers (2)

BigGinDaHouse
BigGinDaHouse

Reputation: 1366

you have two options:

For HQL you need following:

@Query("select * from Visit v where YEARWEEK('v.date') = YEARWEEK(curdate())")

You can have native query also, in that case you need following (not the best way for Spring applications, though can be used for some cases)

@Query(value = "select * from Visit where YEARWEEK('date') = YEARWEEK(curdate())", nativeQuery = true)

Upvotes: 4

andolsi zied
andolsi zied

Reputation: 3791

The @Query annotation allows to execute native queries by setting the nativeQuery flag to true.

@Query("select v from Visit v where YEARWEEK('date') = YEARWEEK(curdate())", nativeQuery = true)

see spring-data docs Using @Query section.

Upvotes: 3

Related Questions