Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Method is only allowed for a query. Use execute or executeUpdate instead of executeQuery. Java/Spring

I have my repository where I have this function:

@Query(nativeQuery = true, value = "DELETE FROM credit_account WHERE ccid = :ccid")
void deleteAllByccid(
        @Param("ccid") Long ccid
);

When I trigger this function I get error:

org.h2.jdbc.JdbcSQLNonTransientException: Method is only allowed for a query. Use execute or executeUpdate instead of executeQuery; SQL statement:
DELETE FROM credit_account WHERE ccid = ? [90002-200]

If this information will be useful, I have join in this table on ccid (ccid is for company client id) and it looks like this. But at the same time, I can do selects also using this column and it works just fine:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@ManyToOne
@JoinColumn(name = "ccid")
private CompanyClient companyClient;

I see, that I need to use execute or executeUpdate, but I only learn it, I tried to find it in internet and haven’t found nothing. So, how can I fix this error? Thanks for answers!

Here is full code of repo:

package com.bankapp.bankwebapplication.repositories;

import com.bankapp.bankwebapplication.models.DebitAccount;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface DebitAccountRepository extends CrudRepository<DebitAccount, Long> {
    Optional<DebitAccount> findById(Long id);
    List<DebitAccount> findAll();
    List<DebitAccount> findAllById(Long id);

    @Query(nativeQuery = true, value = "SELECT * FROM debit_account WHERE ccid = :ccid")
    List<DebitAccount> findAllByccid(
            @Param("ccid") Long ccid
    );

    @Query(nativeQuery = true, value = "SELECT * FROM debit_account WHERE pcid = :pcid")
    List<DebitAccount> findAllBypcid(
            @Param("pcid") Long pcid
    );

    @Modifying
    @Query(nativeQuery = true, value = "DELETE FROM debit_account WHERE ccid = :ccid")
    void deleteAllByccid(
            @Param("ccid") Long ccid
    );

    @Modifying
    @Query(nativeQuery = true, value = "DELETE FROM debit_account WHERE pcid = :pcid")
    void deleteAllBypcid(
            @Param("pcid") Long pcid
    );
}

>Solution :

You can see the answer to your question has been answered here.

org.h2.jdbc.JdbcSQLException: Method is only allowed for a query

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading