jpa-criteria

JPA CriteriaBuilder like on double

℡╲_俬逩灬. 提交于 2020-08-08 18:51:26
问题 I'm trying to retrieve data out of a legacy database. The column in the table is defined as a DECIMAL(13,0) containing account numbers. The column data type cannot be changed as it will have a major impact on the legacy system. Essentially all programs using the table need to be changed and then recompiled which is not an option. We have a requirement to find all records where the account number contains a value, for example the user could search for 12345 and all accounts with an account

JPA CriteriaBuilder like on double

谁都会走 提交于 2020-08-08 18:51:12
问题 I'm trying to retrieve data out of a legacy database. The column in the table is defined as a DECIMAL(13,0) containing account numbers. The column data type cannot be changed as it will have a major impact on the legacy system. Essentially all programs using the table need to be changed and then recompiled which is not an option. We have a requirement to find all records where the account number contains a value, for example the user could search for 12345 and all accounts with an account

JPA CriteriaQuery implements Spring Data Pageable.getSort()

核能气质少年 提交于 2020-01-03 16:38:43
问题 I used JPA CriteriaQuery to build my dynamic Query and pass in a Spring Data Pageable object: sort=name,desc At the backend I have a method in my Repository to support dynamic query: public Page<User> findByCriteria(String username, Pageable page) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<User> cq = cb.createQuery(User.class); Root<User> iRoot = cq.from(User.class); List<Predicate> predicates = new ArrayList<Predicate>(); if (StringUtils.isNotEmpty(username)) { predicates

JPA CriteriaQuery implements Spring Data Pageable.getSort()

自古美人都是妖i 提交于 2020-01-03 16:37:04
问题 I used JPA CriteriaQuery to build my dynamic Query and pass in a Spring Data Pageable object: sort=name,desc At the backend I have a method in my Repository to support dynamic query: public Page<User> findByCriteria(String username, Pageable page) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<User> cq = cb.createQuery(User.class); Root<User> iRoot = cq.from(User.class); List<Predicate> predicates = new ArrayList<Predicate>(); if (StringUtils.isNotEmpty(username)) { predicates

JPA Specifications by Example

为君一笑 提交于 2019-12-30 06:13:06
问题 Spring Boot here. I'm trying to wrap my head around JpaRepositories and Specifications when used in the context of implementing complex queries and am struggling to see the "forest through the trees" on several items. A canonical example of a Specification is as follows: public class PersonSpecification implements Specification<Person> { private Person filter; public PersonSpecification(Person filter) { super(); this.filter = filter; } public Predicate toPredicate(Root<Person> root,

Using Common Table Expression (CTE) in JPA Criteria API

允我心安 提交于 2019-12-24 18:36:51
问题 WITH cte AS ( select A.A_ID, B.Lib, A.Lib, C.Lib, (SELECT count(*) FROM X WHERE A.A_ID = X.A_ID) AS countX, (SELECT count(*) FROM Y WHERE A.A_ID = Y.A_ID) AS countY, (SELECT count(*) FROM Z WHERE A.A_ID = Z.A_ID) AS countZ from A left outer join C on A.C_ID=C.C_ID left outer join B on A.B_ID=B.B_ID ) select * from cte where countX = 2 AND countY = 3 Order BY countZ DESC; I know how to create the sql query inside the CTE but I don't have any idea how to create the CTE in JPA Criteria API (

JPA Criteria API filter subentities

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 09:48:55
问题 The code example: @Entity public class Event { @Id @GeneratedValue private Long id; private String name; @OneToMany(mappedBy="event") private List<Actions> actions; } @Entity public class Action { @Id @GeneratedValue private Long id; private String name; private Date date; @ManyToOne @JoinColumn(name = "event_id") private Event event; } public class EventSpecification { public static Specification<Event> findByCriteria(EventSearchCriteria criteria) { return (root, criteriaQuery,

LocalDate between using JPA 2.1 Criteria API

雨燕双飞 提交于 2019-12-23 03:49:04
问题 In my entity I have two fields : private LocalDate startDate = LocalDate.of(1900, 1, 1); private LocalDate endDate = LocalDate.of(3000, 1, 1); Using JPA Criteria API I want to select entities where LocalDate.now() > startDate and LocalDate.now() < endDate . I tried as following : predicates.add(builder.greaterThan(LocalDate.now(), path.<LocalDate> get(Entity_.startDate))); predicates.add(builder.lessThan(builder.currentDate(), path.<LocalDate> get(Entity_.endDate))); But I get this error :

How to pass variable number of parameters to Spring Data/Hibernate/JPQL query

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 04:22:28
问题 I need to pass in a variable number of parameters to a spring/JPA repo to mimick something like this. select * from myTable where name like '%SOME_VALUE%' or name like '%SOME_OTHER_VALUE%' or name like '%SOME_OTHER_VALUE2%' or an unknown number of other values So far I haven't been able to determine what the correct way to to do this is. I'm using Spring 4.3.7, Hibernate 5.2.9, and Spring Data 1.11.1. I've googled around and it doesn't appear that there's way to do this with a normal CRUD

UserType api methods are not called from Criteria Builder query in hibernate

久未见 提交于 2019-12-11 08:08:23
问题 I have a scenario where i use a custom user type for a entity field in hibernate using the org.hibernate.usertype.UserType . It's a date conversion to UTC import org.hibernate.usertype.ParameterizedType; import org.hibernate.usertype.UserType; public final class UTCTimestampType implements ParameterizedType, UserType {..} My entity class is like the below @Entity public class Person extends AbstractPerson { @Column(name = "BIRTH_DT_TM") @Type(type = "com.myexample.hibernate.types