user51010
user51010

Reputation: 141

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.

Upvotes: 3

Views: 2748

Answers (1)

tmarwen
tmarwen

Reputation: 16354

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.

Upvotes: 2

Related Questions