创建没有实体的Spring存储库 [英] Create spring repository without entity

查看:82
本文介绍了创建没有实体的Spring存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用spring数据存储库接口执行本机查询-由于复杂度低,我认为这种方式是最简单的.

I want to use spring data repository interface to execute native queries - I think this way is the simplest because of low complexity.

但是当扩展接口ex时. CrudRepository<T, ID>我需要写T-我的实体,该实体不可用.

But when extending interface ex. CrudRepository<T, ID> I need to write T - my entity, which is not available.

我的本​​机查询不返回任何具体实体,那么在没有实体的情况下创建spring存储库的最佳方法是什么?

My native queries does not return any concrete entity, so what is the best way to create spring repository without entity?

推荐答案

CrudRepositoryJpaRepository不能在没有<Entity,ID>对的情况下工作.

CrudRepository or JpaRepository were not designed to work without an <Entity,ID> pair.

最好创建一个自定义存储库,注入EntityManager并从那里查询:

You are better off creating a custom repo, inject EntityManager and query from there:

  @Repository
  public class CustomNativeRepositoryImpl implements CustomNativeRepository {

    @Autowired
    private EntityManager entityManager;

    @Override
    public Object runNativeQuery() {
        entityManager.createNativeQuery("myNativeQuery")
         .getSingleResult();
    }
}

这篇关于创建没有实体的Spring存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆