criteria-api

Implement complex search feature using Spring BOOT REST and Spring Data JPA using Criteria API

放肆的年华 提交于 2019-12-04 19:16:13
I need to implement complex search feature using Spring Boot REST and Spring Data JPA using Criteria API. I need to provide RPIs like below /college?select=*&where=name:DemoCollege and location:LA and staff{firstName:foo, lastName:boo, workExp>10} and type in [1,2,3] Collage object has name , location , type fields and staff list. It has onetomany relationship with Staff so College can have one or many Staff members. based on the uri I need to build query using criteria api. I am finding it very complex to implement the toPredicate() method of org.springframework.data.jpa.domain.Specification

JPA Criteria query Path.get left join is it possible

╄→гoц情女王★ 提交于 2019-12-04 19:16:12
问题 I have a question regarding JPA criteria. Here is my JPA criteria query: CriteriaBuilder criteriaBuilder = getEm().getCriteriaBuilder(); CriteriaQuery<InventoryItemSumReport> query = criteriaBuilder.createQuery(InventoryItemSumReport.class); Root<InventoryItemDetail> from = query.from(InventoryItemDetail.class); Join<InventoryItemDetail, InventoryItem> joinItem = from.join(InventoryItemDetail_.inventoryItem); Predicate where = criteriaBuilder.lessThanOrEqualTo(from.get(InventoryItemDetail_

How to do a distinct count in JPA critera API?

血红的双手。 提交于 2019-12-04 17:26:11
问题 I would like to do this but with the criteria API instead: select count(distinct e) from Event e, IN(e.userAccessPermissions) p where p.principal = :principal and p.permission in (:permissions) Any ideas? 回答1: You can use countDistinct on CriteriaBuilder criteriaQuery.select(criteriaBuilder.countDistinct(entityRoot)) 回答2: Like this? Criteria crit = session.createCriteria(Event.class): crit.createAlias("userAccessPermissions", "p"); crit.add(Restrictions.eq("p.principal", principal); crit.add

Get rid of if-else ladder when creating JPA criteria query based on sort/filter fields of LazyDataModel

感情迁移 提交于 2019-12-04 16:03:05
I'm using, JPA 2.0 Mojarra 2.1.9 JSF component library, Primefaces 3.5. MySQL 5.6.11 I have a table in MySQL database named state_table with three columns as an example. state_id (BigInt) state_name (Varchar) country_id (BigInt) state_id is a auto-generated primary key and country_id is a foreign key that references a primary key of the country table. This table is mapped by its corresponding entity class named StateTable and the data held by this table are displayed in a Primefaces DataTable , <p:dataTable>...</p:dataTable> . The DataTable column header contains a clickable sort area, <div>

EntityManager injection - NullPointerException

只谈情不闲聊 提交于 2019-12-04 14:05:58
问题 In my Spring+JPA/Hibernate+Wicket app, I have a QueryBuilder bean that I want to use in one of my DAOs which generates a typed query with the help of Criteria API: @Service(value="inboxQueryBuilder") public class InboxQueryBuilder { @PersistenceContext EntityManager em; CriteriaBuilder cb; public InboxQueryBuilder() { cb = em.getCriteriaBuilder(); } public TypedQuery<App> getQueryForApps(AppSearchObject aso) { ... } ... } However, when I run the app, I get a null pointer exception for line:

JPA criteria API order by NULL last

折月煮酒 提交于 2019-12-04 13:16:32
问题 I use JPA criteria API to fetch records from the datebase. I have entity Record with field dateTime which can be null. I would code: public List<Record> find(RecordFilter recordFilter, int page, int pageSize) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Record> criteriaQuery = criteriaBuilder.createQuery(Record.class); Root<Record> recordRoot = criteriaQuery.from(Record.class); /* * JOINS. Left Joins are used for optional fields, or fields inside of

JPA + Hibernate count(*) using CriteriaBuilder - with generatedAlias

走远了吗. 提交于 2019-12-04 12:08:00
When trying to create a count(*) type query using CriteriaBuilder I get the below alias problem. What changes should I make to the code below to get the count? Constraints: I have to use CriteriaBuilder/Query as the where clause has to be built dynamically based on values. I need only COUNT, not the list of objects in memory. Code sample snippet: Class<ReqStatCumulative> entityClass = ReqStatCumulative.class; @Override public long getCountForAlertConfig(AlertConfig cfg) { long count = 0L; if (null != cfg) { CriteriaBuilder qb = entityManager.getCriteriaBuilder(); Metamodel model =

jpa lazy fetch entities over multiple levels with criteria api

末鹿安然 提交于 2019-12-04 11:05:40
问题 I am using JPA2 with it's Criteria API to select my entities from the database. The implementation is OpenJPA on WebSphere Application Server. All my entities are modeled with Fetchtype=Lazy. I select an entity with some criteria from the database and want to load all nested data from sub-tables at once. If I have a datamodel where table A is joined oneToMany to table B, I can use a Fetch-clause in my criteria query: CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<A> cq

Creating queries using Criteria API (JPA 2.0)

僤鯓⒐⒋嵵緔 提交于 2019-12-04 10:49:28
问题 I'm trying to create a query with the Criteria API from JPA 2.0, but I can't make it work. The problem is with the "between" conditional method. I read some documentation to know how I have to do it, but since I'm discovering JPA, I don't understand why it does not work. First, I can't see "creationDate" which should appear when I write "Transaction_." I thought it was maybe normal, since I read the metamodel was generated at runtime, so I tried to use 'Foo_.getDeclaredSingularAttribute(

Union All and Sum with JPA CriteriaBuilder

↘锁芯ラ 提交于 2019-12-04 09:52:34
问题 I am trying to convert a native SQL query to use the Criteria API in JPA 2.0. I have found a lot of Criteria API examples on Google, but I am having a really hard time putting all of the pieces together. I'm hoping that a more experienced person will be able to help me out. The native query looks like this: select sum(amount) from firstTable, secondTable where firstTable.id = secondTable.id and amount <> 0 and firstTable.id = ? union all select sum(amount) from firstTable, thirdTable where