我正在使用角度js,spring mvc和spring jpa數據處理web應用程序。 我想知道是否有類似的標準和detachedcriteria(休眠)來構建與春季jpa數據的高級查詢。春季數據標準
春季數據標準
回答
是的,你可以使用Specifications,它基本上使用Criteria API(顯然,因爲Spring Data JPA只是JPA的一個包裝)。
保存引用2個其他對象的對象的最佳方式是什麼?如何只用一個查詢來做到這一點。 (不需要從數據庫中獲取它們)。 – housseminfo
你應該問這是一個新問題(並接受我的答案,如果它可以幫助你解決第一個問題)。 – dunni
你也應該閱讀http://stackoverflow.com/help,尤其是「詢問」部分。 – dunni
沒有什麼阻止你還在使用標準
@Repository
public interface FooRepository extends JpaRepository<Foo, Long>, FooRepositoryCustom {
}
interface FooRepositoryCustom {
public List<Foo> findByBar(Bar bar);
}
class FooRepositoryImpl implements FooRepositoryCustom {
@PersistenceContext
protected EntityManager em;
@Transactional(readOnly = true)
public List<Foo> findByBar(Bar bar) {
Criteria crit = em.unwrap(Session.class).createCriteria(Foo.class);
crit.add(Restrictions.eq("name", bar.getName()));
...
crit.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);
List<Foo> foos = crit.list();
return foos;
}
}
您可以使用標準使用Spring的數據,你不需要自定義庫,你可以使用JpaSpecificationExecutor,這裏一個例子:
你的資料庫:
@Repository("yourRepository")
public interface YourRepository extends JpaRepository, JpaSpecificationExecutor
{
}
您服務
@Override
public List<YourModel> yourDataModel getAllEntitiesByAttr(String attrValue){
List<YourModel> yourDataModel = null;
try {
Specification specification=getAndSpecByAttribute("attribute",attrValue);
List list = userRepository.findAll(specification);
yourDataModel =orikaMapper.mapAsList(list, YourModel.class);
} catch (Exception e) {
throw e;
}
return yourDataModel;
}
private Specification getAndSpecByAttribute(String attribute, String valueAttribute){
return new Specification() {
@Override public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
Path path = root.get(attribute);
return cb.equal(path, valueAttribute);
};
};
}
這就夠了。
- 1. 如何添加多條件和條件春季數據標準
- 2. 春數據JPA標準像
- 3. 春季數據neo4j 3.0.0 - 標籤支持
- 4. 春季MVC春季:綁定標籤
- 5. 春季數據和junit
- 6. 春季數據Solr與Querydsl
- 7. 春季分頁數據
- 8. 春季數據JPA其餘
- 9. 如何在春季數據
- 10. 春季和春季數據JPA不協同工作
- 11. 春季數據併發數據訪問
- 12. 春季引導指標與春季雲指標
- 13. 春季選擇標籤
- 14. 春季豆標記錯誤
- 15. 基於標準的授權與春季安全檢查?
- 16. 春季Mongo標準查詢兩次相同的字段
- 17. 春季Mongo標準問題與運營商
- 18. 春季
- 19. 春季
- 20. 春季
- 21. 春季
- 22. 春季
- 23. 春季
- 24. 上查詢春季數據錯誤
- 25. 春季jndi數據源設置
- 26. 沒有數據庫的春季啓動
- 27. 春季數據CrudRepository作爲方法豆
- 28. 春季數據CrudRepository findBy關係
- 29. 春季數據最後一筆記錄
- 30. 春季數據休息 - 緩存
[Dynamic spring data jpa repository query with any AND clauses]的可能重複(http://stackoverflow.com/questions/28874135/dynamic-spring-data-jpa-repository-query-with-arbitrary-andclauses ) – iamiddy