1

I'm writing a custom query with Spring Data JDBC:

public interface AuditRecordRepository extends CrudRepository<AuditRecord, Long> {

@Modifying
@Query("DELETE FROM audit_records ar WHERE ar.created_on <= :date;")
int retainDataBefore(@Param("date") Timestamp retainDate);

}

AuditRecord:

@Entity
@Table(name = "audit_records")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class AuditRecord {
  @Id 
  @Column(name="id")
  private Long id;

  @Column(name = "created_on")
  private Timestamp date;
}

When I run the application it says:

No property for "retainData" found for type "AuditRecord" found.

treating my method name as a property. What am I doing wrong?

lzy917
  • 43
  • 3
  • You verified you are using the JDBC versions of `@Modifying` and `@Query`, I assume? Can you make a reproducer available on GitHub? – Jens Schauder Jul 04 '23 at 06:30

2 Answers2

1

I figured it out in the end. It was because I was using annotations with the same name but from incorrect packages. Sorry for such a simple mistake. I hope I didn't waste too much time for people who are trying to help.

lzy917
  • 43
  • 3
0

By default, the query definition uses JPQL.

Then your query should be this:

@Modifying
@Transactional
@Query("DELETE FROM AuditRecord ar WHERE ar.date = :date")
int retainDataBefore(@Param("date") Timestamp retainDate);

Otherwise you can use Native query: Then your query should be this:

@Modifying
@Transactional
@Query(
        value = "DELETE FROM audit_records ar WHERE ar.created_on = :date",
        nativeQuery = true)
int retainDataBefore1(@Param("date") Timestamp retainDate);

Note: Please remove ";" inside your query. if you have definition @Transactional in your service class then remove from repository interface.

  • 1
    But I'm using Spring Data JDBC (not JPA), it doesn't have JPQL suppport if I recall correctly. – lzy917 Jul 03 '23 at 14:33
  • In your above code is Spring Data JPA code, if you want to use Spring JDBC this case, should be use JdbcTemplate or Named Parameters, please check this tutorial for Spring JDBC https://www.baeldung.com/spring-jdbc-jdbctemplate – Henry Xiloj Herrera Jul 03 '23 at 15:46
  • 1
    @Herny These are three different things, Spring Data JPA, Spring Data JDBC and Spring JDBC. Check this out https://stackoverflow.com/questions/51923060/spring-jdbc-vs-spring-data-jdbc-and-what-are-they-supporting – lzy917 Jul 03 '23 at 19:13
  • @Izy917, yes, you have reason, however It is part of the larger Spring Data family of projects, which also includes Spring Data JPA, then you can use native query, thank you for your inputs. – Henry Xiloj Herrera Jul 03 '23 at 20:03
  • Spring Data JDBC does not support JPQL nor does it have a way to distinguish _native_ from _non_native_ queries, since that concept does not apply. – Jens Schauder Jul 04 '23 at 06:27
  • @HenryXilojHerrera This one might be interesting as well: https://stackoverflow.com/questions/42470060/spring-data-jdbc-spring-data-jpa-vs-hibernate/42488593 – Jens Schauder Jul 04 '23 at 06:35
  • @Jens Schauder, I read the post, now for me is clear, although, I created a basic project, Spring Jdbc, Spring Data JPA, Spring Data JDBC for check the differences. thank you. – Henry Xiloj Herrera Jul 04 '23 at 16:57