英文:
Spring Data JDBC QueryCreationException. Spring Data treating my custom query name as a property name
问题
我正在使用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;
}
当我运行应用程序时,它报错:
找不到类型"AuditRecord"的属性"retainData"。
将我的方法名称视为属性。我做错了什么?
英文:
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?
答案1
得分: 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.
答案2
得分: 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.
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论