简体   繁体   English

CrudRepository-按日期删除

[英]CrudRepository - delete by Date

Could you please help me with the following problem? 您能帮我解决以下问题吗?

I have an Entity: 我有一个实体:

@Entity
public class Logging {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @Column(name = "domain")
    private String domain;

    @Column(name = "time_stamp")
    private Date timeStamp;

I have also: 我也有:

public interface LoggingRepository extends CrudRepository<Logging, Integer> {

    // @Query("Delete from Logging l WHERE l.timeStamp < ?1 AND l.domain =?2")
    void deleteLoggingTimeStampBeforeAndByDomain(Date timeStamp, String domain);
}

Using CrudRepository I want to delete Logging records that are less than dateX and Logging.domain = myDomain. 我想使用CrudRepository删除小于dateX和Logging.domain = myDomain的日志记录。 Something like: 就像是:

DELETE from Logging l WHERE l.time_stamp < '2018-05-22 21:32:26' AND l.domain = 'abc.com'. 

In database column time_stamp looks like 2018-05-22 21:32:26 在数据库列中time_stamp看起来像2018-05-22 21:32:26

In my MySQL table Logging time_stamp is of type TIMESTAMP. 在我的MySQL表中,日志记录time_stamp的类型为TIMESTAMP。

I use Spring Boot. 我使用Spring Boot。 Actually I get the following error: 实际上我收到以下错误:

There was an unexpected error (type=Internal Server Error, status=500). 发生意外错误(类型=内部服务器错误,状态= 500)。 Parameter value [2018-05-22 21:32:36.0] did not match expected type [java.lang.String (n/a)]; 参数值[2018-05-22 21:32:36.0]与预期的类型[java.lang.String(n / a)]不匹配; nested exception is java.lang.IllegalArgumentException: Parameter value [2018-05-22 21:32:36.0] did not match expected type [java.lang.String (n/a)] 嵌套异常是java.lang.IllegalArgumentException:参数值[2018-05-22 21:32:36.0]与预期的类型[java.lang.String(n / a)]不匹配

EDIT: When I chang emy method to: 编辑:当我更改emy方法为:

 @Query("Delete from Logging l WHERE timeStamp < :timeStamp AND domain =:domain")
    void deleteLoggingTimeStampBeforeAndByDomain(@Param("timeStamp") Date timeStamp, @Param("domain") String domain);

I get the error: 我收到错误:

org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [Delete from entity.Logging l WHERE timeStamp < :timeStamp AND domain =:domain] org.hibernate.hql.internal.QueryExecutionRequestException:DML操作不支持[从entity.Logging删除l WHERE timeStamp <:timeStamp AND domain =:domain]

Do you have any idea? 你有什么主意吗? I've spent many hours but unfortunately without success. 我花了很多时间,但不幸的是没有成功。 I've tried with @Query and special naming convention without @Query . 我尝试了@Query和没有@Query特殊命名约定。

OK, I have the solution: I had to add @Modifying just like: 好的, 我有解决方案:我必须像这样添加@Modifying:

@Modifying
@Query("Delete from Logging l WHERE timeStamp < :timeStamp AND domain =:domain")
    void deleteLoggingTimeStampBeforeAndByDomain(@Param("timeStamp") Date timeStamp, @Param("domain") String domain);

Sorry for all the fuss. 对不起,大惊小怪。

Thank you. 谢谢。 Matley 马特利

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM