java - 未找到 native micronaut 数据插入查询的可能实现

我正在将 spring boot 应用程序迁移到 micronaut,偶然发现 micronaut 数据存在问题。 当使用在 spring boot 数据中工作的 native 查询时,我在尝试将一些数据插入关联表的查询中遇到编译错误。

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

其他原生查询(选择、删除)没有问题,生成的方法也是如此。 以下是使用上述方法的 repo 协议(protocol):

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);
}

my_entity_my_entity2 没有实体类,但它在 Spring 有效,所以我认为这不是问题。

预先感谢您的帮助。

最佳答案

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

的确,这就是问题。

所有 io.micronaut.data.repository.GenericRepository 都需要一个相应的实体类型(必须自省(introspection),它是 Micronaut Data JPA 还是 Micronaut数据 JDBC 实现。

剩下的解决方案是实现自定义 JpaRepository 子类型并使用注入(inject)的 EntityManagerJpaRepositoryOperations 执行自定义在保留默认拦截方法的同时执行查询:

@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();
    }
}

然后您可以注入(inject)您的 MyEntityRepository bean 并调用您的自定义查询方法。

关于java - 未找到 native micronaut 数据插入查询的可能实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67005078/

相关文章:

sql-server - SQL 服务器 : splitting a row into severa

validation - VeeValidate 4 : two forms on one page

azure - 对于高级、低延迟、带有搜索功能的大量小型 json 文件,Azure BlockBl

google-apps-script - 在多个工作表中搜索一个词

java - 在 Spring Cloud Gateway 中获取真实的客户端 IP 地址

spring-boot - StreamingResponseBodyReturnValueHand

mongodb - 如何在 Rust 中使用 Mongodb::cursor?

typescript - 类型错误 : Property 'currentTime' does no

ios - FaSTLane 设置 - 致命 : could not read Password f

javascript - 如果没有 .js 扩展名,则为 ERR_MODULE_NOT_FOUND