有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java创建没有实体的spring存储库

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

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

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


共 (6) 个答案

  1. # 1 楼答案

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

    您最好创建一个定制的回购协议,从中插入EntityManager和查询:

      @Repository
      public class CustomNativeRepositoryImpl implements CustomNativeRepository {
    
        @Autowired
        private EntityManager entityManager;
    
        @Override
        public Object runNativeQuery() {
            entityManager.createNativeQuery("myNativeQuery")
             .getSingleResult();
        }
    }
    
  2. # 2 楼答案

    您可以使用@Repository注释您的实现,并获得EntityManager的实例

    public interface ProductFilterRepository {
        Page<Product> filter(FilterTO filter, Pageable pageable);
    }
    
    
    
    @Repository
    @AllArgsConstructor
    public class ProductFilterRepositoryImpl implements ProductFilterRepository {
    
        private final EntityManager em;
    
        @Override
        public Page<Product> filter(FilterTO filter, Pageable pageable) {
            CriteriaBuilder cb = em.getCriteriaBuilder();
            CriteriaQuery<Product> cq = cb.createQuery(Product.class);
            Root<Product> root = cq.from(Product.class);
            List<Predicate> predicates = new ArrayList<>();
    
            if (filter.getPriceMin() != null) {
                predicates.add(cb.ge(root.get("price"), filter.getPriceMin()));
            }
            if (filter.getPriceMax() != null) {
                predicates.add(cb.le(root.get("price"), filter.getPriceMax()));
            }
            if (filter.getBrands() != null && !filter.getBrands().isEmpty()) {
                predicates.add(root.get("brand").in(filter.getBrands()));
            }
            if (filter.getCategories() != null && !filter.getCategories().isEmpty()) {
                predicates.add(root.get("category").in(filter.getCategories()));
            }
            cq.where(predicates.toArray(new Predicate[0]));
            TypedQuery<Product> tq = em.createQuery(cq);
            tq.setMaxResults(pageable.getPageSize());
            tq.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
    
            CriteriaQuery<Long> countCq = cb.createQuery(Long.class);
            countCq.select(cb.count(countCq.from(Product.class)));
            countCq.where(predicates.toArray(new Predicate[0]));
            TypedQuery<Long> countTq = em.createQuery(countCq);
            Long count = countTq.getSingleResult();
    
            return new PageImpl<>(tq.getResultList(), pageable, count);
        }
    }
    
  3. # 4 楼答案

    我认为,当您没有用于本机查询结果集的具体实体类时,可以考虑使用JdbcTemplate。使用JdbcTemplate查询数据需要resultset的POJO类和实现POJO类的RowMapper接口的映射器

  4. # 5 楼答案

    目前,JPA中没有只使用本机或JPQL/HQL查询(使用@Query表示法)创建存储库的功能。要解决此问题,您可以创建一个虚拟对象以插入扩展接口,如下所示:

    @Entity
    public class RootEntity {
        @Id
        private Integer id;
    }
    
    @Repository
    public interface Repository extends JpaRepository<RootEntity, Integer> {
    }
    
  5. # 6 楼答案

    这对我们有用。请参阅实体管理器

    https://www.baeldung.com/hibernate-entitymanager

    @Repository
    public class MyRepository {
    
        @PersistenceContext
        EntityManager entityManager;
    
        public void doSomeQuery(){
            Query query = entityManager.createNativeQuery("SELECT foo FROM bar");
            query.getResultsList()
            ...
        }
    
    }
    

    顺便说一句,我认为这里甚至不需要@Repository注释