3

I want to delete records which are one day older , I am using SQL query in Spring data JPA as below:

@Query(value = "delete from tableName data where data.CREATION_DATE < TRUNC(SYSDATE) - 1", nativeQuery = true)
    void deleteRecordsOlderThanYesterday();

I am getting exception as:

Caused by: org.springframework.orm.jpa.JpaSystemException: Could not extract result set metadata Caused by: org.hibernate.HibernateException: Could not extract result set metadata

Caused by: java.sql.SQLSyntaxErrorException: ORA-00900: invalid SQL statement

What is wrong with above code or is there any other way to do it in HQL?

Please let me know.

2 Answers 2

7

Whenever you are defining queries from SQL Statements (UPDATE, DELETE) You need to use @Modifying annotation (from org.springframework.data.jpa.repository). Try this:

@Modifying
@Query(value = "delete from tableName data where data.CREATION_DATE < TRUNC(SYSDATE) - 1", 
        nativeQuery = true)
void deleteRecordsOlderThanYesterday();
Sign up to request clarification or add additional context in comments.

1 Comment

@infiniteRefactor...thanks for your answer, i think u r right and i will accept ur answer after checking ur answer on my sys
1

you'd better use JpaRepository and use findByStartDateBefore and you dont need to use @Query

 public List<yourResult> findByStartDateBefore(Date date);

for more information you can see spring doc http://docs.spring.io/spring-data/jpa/docs/1.3.4.RELEASE/reference/html/jpa.repositories.html

and this question Object Recovery via a date with spring data

1 Comment

Hello Azzabi, I am able to delete using deleteByCreationDateBefore , thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.