Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring-data-jdbc deleteBy<field> method return extraordinary result type

I try declare and use deleteBy method with spring-data-jdbc repositories like this

public interface TokenRepository extends CrudRepository<OpToken, Long> {
   void deleteByBreed(Long breed);
}

When i tried to call method

private TokenRepository tokenRepository;
...
...
tokenRepository.deleteByBreed(123L);

I got exception: MethodNotFoundException:

java.lang.NoSuchMethodException: void.()

I decided, that delete method should return number of rows it processed. So, i rewrite my repository interface like this

public interface TokenRepository extends CrudRepository<OpToken, Long> {
   long deleteByBreed(Long breed);
}

But now i have got another exception: org.springframework.jdbc.IncorrectResultSetColumnCountException: Incorrect column count: expected 1, actual 4

It's looks like method return entity or list of entities it try to delete. But i do not need them. How can i declare this method in my case?

By Entity looks like this:

@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;
  }
}
like image 914
MadMax Avatar asked Nov 15 '25 13:11

MadMax


2 Answers

With the current version derived delete queries aren't supported yet. Watch https://github.com/spring-projects/spring-data-jdbc/issues/771 to get notified when this changes.

The solution provided by @MadMax is correct: used a dedicated query:

@Modifying
@Query("delete from account.op_token t where t.breed = :breed")
void deleteByBreed(@Param("breed") Long breed);
like image 187
Jens Schauder Avatar answered Nov 18 '25 05:11

Jens Schauder


It's only one worked

   @Modifying
   @Query("delete from account.op_token t where t.breed = :breed")
   Long(or void) deleteByBreed(@Param("breed") Long breed);
like image 32
MadMax Avatar answered Nov 18 '25 04:11

MadMax



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!