我尝试在spring-data-jdbc存储库中声明和使用deleteBy方法,如下所示
public interface TokenRepository extends CrudRepository<OpToken, Long> {
void deleteByBreed(Long breed);
}当我尝试调用方法时
private TokenRepository tokenRepository;
...
...
tokenRepository.deleteByBreed(123L);我得到了异常: MethodNotFoundException:
java.lang.NoSuchMethodException: void。()
我决定,delete方法应该返回它处理的行数。因此,我重写了我的仓库接口,如下所示
public interface TokenRepository extends CrudRepository<OpToken, Long> {
long deleteByBreed(Long breed);
}但现在我得到了另一个异常: org.springframework.jdbc.IncorrectResultSetColumnCountException:不正确的列数:预期为1,实际为4
它看起来像是方法返回实体或它试图删除的实体列表。但我不需要他们。在我的例子中,我如何声明这个方法?
按实体如下所示:
@Data
public class OpToken implements Persistable<Long> {
@Transient
private boolean newEntity;
@Id
@Column("jti")
private Long jti;
@Column("breed")
private Long breed;
@Column("id_account")
private Long idAccount;
@Column("exp")
private Date exp;
@Override
public Long getId() {
return jti;
}
@Override
public boolean isNew() {
return newEntity;
}
}发布于 2021-06-10 18:49:27
它只有一个能用
@Modifying
@Query("delete from account.op_token t where t.breed = :breed")
Long(or void) deleteByBreed(@Param("breed") Long breed);发布于 2021-06-11 13:46:34
在当前版本中,还不支持派生的delete查询。请关注https://github.com/spring-projects/spring-data-jdbc/issues/771,以便在此更改时获得通知。
@MadMax提供的解决方案是正确的:使用了专用查询:
@Modifying
@Query("delete from account.op_token t where t.breed = :breed")
void deleteByBreed(@Param("breed") Long breed);发布于 2021-07-02 01:50:14
但是,只有当您的实体没有值对象(不是@Embedded)时,查询才有效。我有一个带有值对象的Customer实体,所以我在我的服务中使用了这个方法(customerId是唯一的):
@Transactional
public void deleteByCustomerId(final String customerId) {
customerRepository.findByCustomerId(customerId).ifPresent(customerRepository:delete);
}https://stackoverflow.com/questions/67916289
复制相似问题