Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No possible implementation found for native micronaut data insert queries

I'm in the process of migrating a spring boot application to micronaut and stumbled upon a problem with micronaut data. When using native queries which worked in spring boot data I get a compilation error for a query in which I try to insert some data into associative table.

Unable to implement Repository method: MyEntityRepository.insertQueryExample(int id, String name). No possible implementations found.

Other native queries (selects, deletes) work no problem, same goes for generated methods. Here's how the repo with said method looks like:

public interface MyEntityRepository extends CrudRepository<MyEntity, Integer> {

    @Query(value = "insert into my_entity_my_entity2 (id, id2)" +
            " values (:id, (select me2.id from my_entity2 os where me2.name = :name))", nativeQuery = true)
    void insertQueryExample(int id, String name);
}

There's no entity class for my_entity_my_entity2 but that worked in spring so I don't think that's a problem.

Thanks in advance for your help.

like image 844
user51010 Avatar asked Feb 17 '26 00:02

user51010


1 Answers

There's no entity class for my_entity_my_entity2 but that worked in spring so I don't think that's a problem.

Indeed, this is the issue.

All io.micronaut.data.repository.GenericRepository expects a respective entity type (which must be introspected, would it be the Micronaut Data JPA or Micronaut Data JDBC implementation.

The solution you are left with is to implement a custom JpaRepository sub-type and use either the injected EntityManager or JpaRepositoryOperations to perform custom query execution while retaining default intercepted methods:

@Repository
public abstract class MyEntityRepository implements CrudRepository < MyEntity, Integer> {

    @Inject
    JpaRepositoryOperations operations;

    @Transactional
    void insertQueryExample(int id, String name) {
        operations.getCurrentEntityManager()
                .createNativeQuery("insert into my_entity_my_entity2 (id, id2)" +
                        " values (:id, (select os.id from my_entity2 os where os.name = :name))")
                .setParameter("id", id)
                .setParameter("name", name)
                .executeUpdate();
    }
}

You can then inject your MyEntityRepository bean and invoke your custom query method.

like image 101
tmarwen Avatar answered Feb 19 '26 12:02

tmarwen



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!