Spring Data JDBC QueryCreationException. Spring Data treating my custom query name as a property name

huangapple go评论52阅读模式
英文:

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&lt;AuditRecord, Long&gt; {

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

}

AuditRecord:

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

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

When I run the application it says:

No property for &quot;retainData&quot; found for type &quot;AuditRecord&quot; 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(&quot;DELETE FROM AuditRecord ar WHERE ar.date = :date&quot;)
int retainDataBefore(@Param(&quot;date&quot;) Timestamp retainDate);

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

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

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

huangapple
  • 本文由 发表于 2023年7月3日 20:54:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定