创建没有实体的Spring存储库 [英] Create spring repository without entity
问题描述
我想使用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?
推荐答案
CrudRepository
或JpaRepository
不能在没有<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屋!