创建不带实体的弹簧存储库

我想使用spring数据存储库接口来执行本机查询 - 我认为这种方式是最简单的,因为复杂性较低。

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

我的本机查询不返回任何具体实体,那么创建没有实体的弹簧存储库的最佳方法是什么?


潇湘沐
浏览 74回答 4
4回答

qq_遁去的一_1

CrudRepository或者不是设计为没有一对。JpaRepository<Entity,ID>您最好创建自定义存储库,注入实体管理器并从那里进行查询:&nbsp; @Repository&nbsp; public class CustomNativeRepositoryImpl implements CustomNativeRepository {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private EntityManager entityManager;&nbsp; &nbsp; @Override&nbsp; &nbsp; public Object runNativeQuery() {&nbsp; &nbsp; &nbsp; &nbsp; entityManager.createNativeQuery("myNativeQuery")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.getSingleResult();&nbsp; &nbsp; }}

慕田峪7331174

目前,JPA 中没有创建仅具有本机或 JPQL/HQL 查询(使用@Query表示法)的存储库的功能。要解决此问题,您可以创建一个虚拟对象以插入到扩展界面中,如下所示:@Entitypublic class RootEntity {&nbsp; &nbsp; @Id&nbsp; &nbsp; private Integer id;}@Repositorypublic interface Repository extends JpaRepository<RootEntity, Integer> {}

幕布斯6054654

这对我们有用。请参阅实体管理器https://www.baeldung.com/hibernate-entitymanager@Repositorypublic class MyRepository {&nbsp; &nbsp; @PersistenceContext&nbsp; &nbsp; EntityManager entityManager;&nbsp; &nbsp; public void doSomeQuery(){&nbsp; &nbsp; &nbsp; &nbsp; Query query = entityManager.createNativeQuery("SELECT foo FROM bar");&nbsp; &nbsp; &nbsp; &nbsp; query.getResultsList()&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; }}顺便说一句,我不认为这里甚至不需要@Repository注释。

凤凰求蛊

您可以使用 注释您的实现,并获取实体管理器的实例。@Repositorypublic interface ProductFilterRepository {&nbsp; &nbsp; Page<Product> filter(FilterTO filter, Pageable pageable);}@Repository@AllArgsConstructorpublic class ProductFilterRepositoryImpl implements ProductFilterRepository {&nbsp; &nbsp; private final EntityManager em;&nbsp; &nbsp; @Override&nbsp; &nbsp; public Page<Product> filter(FilterTO filter, Pageable pageable) {&nbsp; &nbsp; &nbsp; &nbsp; CriteriaBuilder cb = em.getCriteriaBuilder();&nbsp; &nbsp; &nbsp; &nbsp; CriteriaQuery<Product> cq = cb.createQuery(Product.class);&nbsp; &nbsp; &nbsp; &nbsp; Root<Product> root = cq.from(Product.class);&nbsp; &nbsp; &nbsp; &nbsp; List<Predicate> predicates = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; if (filter.getPriceMin() != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; predicates.add(cb.ge(root.get("price"), filter.getPriceMin()));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (filter.getPriceMax() != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; predicates.add(cb.le(root.get("price"), filter.getPriceMax()));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (filter.getBrands() != null && !filter.getBrands().isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; predicates.add(root.get("brand").in(filter.getBrands()));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (filter.getCategories() != null && !filter.getCategories().isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; predicates.add(root.get("category").in(filter.getCategories()));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; cq.where(predicates.toArray(new Predicate[0]));&nbsp; &nbsp; &nbsp; &nbsp; TypedQuery<Product> tq = em.createQuery(cq);&nbsp; &nbsp; &nbsp; &nbsp; tq.setMaxResults(pageable.getPageSize());&nbsp; &nbsp; &nbsp; &nbsp; tq.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());&nbsp; &nbsp; &nbsp; &nbsp; CriteriaQuery<Long> countCq = cb.createQuery(Long.class);&nbsp; &nbsp; &nbsp; &nbsp; countCq.select(cb.count(countCq.from(Product.class)));&nbsp; &nbsp; &nbsp; &nbsp; countCq.where(predicates.toArray(new Predicate[0]));&nbsp; &nbsp; &nbsp; &nbsp; TypedQuery<Long> countTq = em.createQuery(countCq);&nbsp; &nbsp; &nbsp; &nbsp; Long count = countTq.getSingleResult();&nbsp; &nbsp; &nbsp; &nbsp; return new PageImpl<>(tq.getResultList(), pageable, count);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java