亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

創建不帶實體的彈簧存儲庫

創建不帶實體的彈簧存儲庫

瀟湘沐 2022-09-21 16:59:00
我想使用spring數據存儲庫接口來執行本機查詢 - 我認為這種方式是最簡單的,因為復雜性較低。但是當擴展接口ex。 我需要編寫 T - 我的實體,這不可用。CrudRepository<T, ID>我的本機查詢不返回任何具體實體,那么創建沒有實體的彈簧存儲庫的最佳方法是什么?
查看完整描述

4 回答

?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

CrudRepository或者不是設計為沒有一對。JpaRepository<Entity,ID>


您最好創建自定義存儲庫,注入實體管理器并從那里進行查詢:


  @Repository

  public class CustomNativeRepositoryImpl implements CustomNativeRepository {


    @Autowired

    private EntityManager entityManager;


    @Override

    public Object runNativeQuery() {

        entityManager.createNativeQuery("myNativeQuery")

         .getSingleResult();

    }

}


查看完整回答
反對 回復 2022-09-21
?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

目前,JPA 中沒有創建僅具有本機或 JPQL/HQL 查詢(使用@Query表示法)的存儲庫的功能。要解決此問題,您可以創建一個虛擬對象以插入到擴展界面中,如下所示:


@Entity

public class RootEntity {

    @Id

    private Integer id;

}


@Repository

public interface Repository extends JpaRepository<RootEntity, Integer> {

}


查看完整回答
反對 回復 2022-09-21
?
幕布斯6054654

TA貢獻1876條經驗 獲得超7個贊

這對我們有用。請參閱實體管理器


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注釋。


查看完整回答
反對 回復 2022-09-21
?
鳳凰求蠱

TA貢獻1825條經驗 獲得超4個贊

您可以使用 注釋您的實現,并獲取實體管理器的實例。@Repository


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

    }

}


查看完整回答
反對 回復 2022-09-21
  • 4 回答
  • 0 關注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號