criteria-api

JPA: Query an embeddable List inside an entity

假如想象 提交于 2019-12-10 10:08:08
问题 I am trying to "extract" Embeddable classes based on some criterias from a list in an Entity. Either with the help of JPQL or Criteria API. I am not a pro at this, so please help me out. Been googling for 4 hours solid for an answer without any results. These are the classes: @Entity public class PostOffice { @Id private Long id; @ElementCollection(fetch=FetchType.LAZY) @CollectionTable(joinColumns=@JoinColumn(name = "CARRIERID")) private List<PostalCarrier> carriers; } @Embeddable public

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

被刻印的时光 ゝ 提交于 2019-12-09 19:15:37
问题 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

JPA2 Criteria API runtime cast from varchar(25) to decimal

会有一股神秘感。 提交于 2019-12-09 18:29:54
问题 So I've seen all threads on stack overflow on similar subjects but I've found no solution to my problem. I'm trying to create a Criteria query and I get this SQL (1st SQL, simplified): SELECT latitude FROM stations WHERE (ABS(latitude - 45.893227) <= 0.2) but it gives me this error: java.sql.SQLDataException: The resulting value is outside the range for the data type DECIMAL/NUMERIC(31,31). That's because my latitude is of type varchar(25). So this fixes it (2nd SQL): SELECT latitude FROM

How to use key of MAP in Criteria Query?

你说的曾经没有我的故事 提交于 2019-12-09 16:11:27
问题 I have a Bean like this Class TestA { Map<String,TestB> testBMap; } Class TestB { String data; ... } I want to fetch the TestA data along with the map testBMap where key ='test1' . How can i do this using Hibernate. 回答1: The key must be the value of one of the persistent fields of TestB (let's says this field is names "foo"), so this code should work : Criteria criteria = session.createCriteria(TestA.class, "a"); criteria.createAlias("a.testBMap", "b"); criteria.add(Restrictions.eq("b.foo",

JPA criteria query, order on class

余生长醉 提交于 2019-12-09 15:55:46
问题 Is there a way with JPA criteria queries to order on class ? Imagine the following domain objects: abstract class Hobby { ... } class Coding extends Hobby { ... } class Gaming extends Hobby { ... } Using regular QL I was able to do from Hobby h order by h.class But when I apply the same logic on a criteria query, the runtime exception "unknown attribute" occurs. CriteriaQuery<Hobby> criteriaQuery = builder.createQuery(Hobby.class); Root<Hobby> hobbyRoot = criteriaQuery.from(Hobby.class);

CriteriaBuilder - Sum using SelectCase

半腔热情 提交于 2019-12-09 12:46:19
问题 I am trying to perform a summation SQL query like the following: select group_ID, sum(case when user_type = 'Exec' then 1000 when user_type = 'Office' then 10 else 0 end) from subscription group by group_ID; using the following snippet from a hiberate CriteriaBuilder query: criteriaBuilder.sum( criteriaBuilder.selectCase() .when(criteriaBuilder.equal(subscriptionJoin.get(Subscription_.userType), "Exec"),1000) .when(criteriaBuilder.equal(subscriptionJoin.get(Subscription_.userType), "Office")

JPA Criteria API: how to retrieve date in “mm/dd/yyyy” format

流过昼夜 提交于 2019-12-08 18:30:36
I want to retrieve one of my column date in "mm/dd/yyyy" format. This column is currently returning date and time both but i want only date in "mm/dd/yyy" format. Below is my postgresql query that i want to convert to criteria api select DISTINCT c.name as Facility, to_char(begin_exam,'mm/dd/yyyy') as begin_exam from a inner join b on a.rad_exam_id = b.id inner join c on c.id = b.site_id group by c.name,to_char(begin_exam,'mm/dd/yyyy') order by c.name,to_char(begin_exam,'mm/dd/yyyy') I searched on the internet a lot but did't find any solution that will help me. please help me in writing

Conditional where clause in JPA criteria query

天涯浪子 提交于 2019-12-08 16:27:24
问题 I am facing an issue for JPA criteria query. How can I add multiple where clause in my Criteria Query with if else... My Requirement is: CriteriaBuilder builder = getEm().getCriteriaBuilder(); CriteriaQuery<Object> query = builder.createQuery(Object.class); // From Root<EntityClass> entity= query.from(EntityClass.class); // Select query.multiselect(entity.get("id").get("col1"),entity.get("id").get("col2")); // Where Predicate p1 = builder.and(builder.equal(entity.get("col3"), value3));

select specific date format in jpa criteria query

谁说胖子不能爱 提交于 2019-12-08 11:26:32
I want to select date in particular format in multiselect of jpa criteria query like we use select to_char(tn.dbdate,'yyyy-MM-dd') from transaction tn in oracle. I am able to use query.multiselect(cb.function("TO_CHAR",String.class,transaction.get("dbdate")) ); but this returns date in database format i.e. Wed Apr 2 12:20:50 2014 but how to get this in specific date format 'yyyy-MM-dd' In current query no format is given as an argument to TO_CHAR function. That's why it cannot do much else than fall back to default. As documented , more than one arguments can also be passed to database

select specific date format in jpa criteria query

社会主义新天地 提交于 2019-12-08 08:24:51
问题 I want to select date in particular format in multiselect of jpa criteria query like we use select to_char(tn.dbdate,'yyyy-MM-dd') from transaction tn in oracle. I am able to use query.multiselect(cb.function("TO_CHAR",String.class,transaction.get("dbdate")) ); but this returns date in database format i.e. Wed Apr 2 12:20:50 2014 but how to get this in specific date format 'yyyy-MM-dd' 回答1: In current query no format is given as an argument to TO_CHAR function. That's why it cannot do much