criteria-api

Retrieve primary key column definition of a generic entity in JPA

China☆狼群 提交于 2019-12-08 01:38:51
问题 Let say I have a generic method using JPA to list out entities public <T> List<T> list(Class<T> entity) throws Exception { List<T> result = new ArrayList<T>(); CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<T> query = builder.createQuery( entity ); Root<T> root = query.from( entity ); query.select( root ); //possible? query.orderBy(builder.asc(...)); result = em.createQuery( query ).getResultList(); return result; } Is there anyway for us to add orderby to the query and make

Can I use table-valued function as query source in NHibernate?

左心房为你撑大大i 提交于 2019-12-07 07:40:17
问题 I have one question to ask you, dear community, as you may have guessed. So. I want NHibernate to filter the results of a query basing on the evaluation of table-valued sql function. Possible SQL query generated by NHibernate may look similar to the following: SELECT [whatever] FROM [whatever] INNER JOIN dbo.FnMyTableValuedFunction() as MyAlias ON [whatever].FirstDesiredKey = MyAlias.FirstDesiredKey AND [whatever].SecondDesiredKey = MyAlias.SecondDesiredKey Or it can be written this way:

Hibernate Criteria query Collections contains certain elements

淺唱寂寞╮ 提交于 2019-12-07 05:06:17
问题 I have some problems with Criteria . I have a Job class that contains a set of Skills . The problem comes when I want to filter jobs that contain 2 skills, for example, all jobs that contains skill with id 1 and 3. Now I have this: for (Skill skill : job.getSkills()) { ids.add(skill.getId()); } criteria.createAlias("skills", "skill"); criteria.add(Restrictions.in("skill.id", ids)); but it gives me jobs that contain skill 1 or 3, not only those with both skills. How can I do this? UPDATE:

Subquery in where clause with CriteriaQuery

本秂侑毒 提交于 2019-12-07 03:41:43
问题 Can anybody give me some hints on how to put that kind of subquery in a CriteriaQuery ? (I'm using JPA 2.0 - Hibernate 4.x ) SELECT a, b, c FROM tableA WHERE a = (SELECT d FROM tableB WHERE tableB.id = 3) - the second select will always get a single result or null. 回答1: Try something like the following example to create a subquery: CriteriaQuery<Object[]> cq = cb.createQuery(Object[].class); Root tableA = cq.from(TableA.class); Subquery<String> sq = cq.subquery(TableB.class); Root tableB = cq

How to convert SQL Query using CriteriaQuery

别来无恙 提交于 2019-12-06 15:53:48
问题 I want to convert my sql query from SQL to CriteriaQuery, I have this sql query: 1) SELECT * FROM table AS t WHERE id = (SELECT MAX(id) FROM table AS t WHERE t.element_id = 354 AND (name <> 'foo' OR (name = 'bar' AND event = 'foo'))); 2) SELECT tr1.* FROM table AS tr1 INNER JOIN (SELECT MAX(id) AS id FROM table AS tr WHERE tr.element_id = 354 AND (name <> 'foo' OR (name = 'bar' AND event = 'foo'))) AS tr2 ON tr1.id = tr2.id; How is the best way to do this ? public Predicate

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

心已入冬 提交于 2019-12-06 14:46:42
问题 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

How to get an interesect query using CritieraQuery?

纵饮孤独 提交于 2019-12-06 12:47:39
问题 Given @Entity public class Document { @Id @Column(name = "DOCUMENT_ID") private Long id; @ElementCollection @CollectionTable( name="TAG", joinColumns=@JoinColumn(name="DOCUMENT_ID") ) @Column(name="TAG") private Set<String> tags; } find all documents tagged with a specific collection of tags. Essentially, an EclipseLink equivalent of: SELECT d FROM Document d WHERE :tag1 MEMBER OF d.tags INTERSECT SELECT d FROM Document d WHERE :tag2 MEMBER OF d.tags ... SELECT d FROM Document d WHERE :tagn

Retrieve primary key column definition of a generic entity in JPA

与世无争的帅哥 提交于 2019-12-06 12:38:44
Let say I have a generic method using JPA to list out entities public <T> List<T> list(Class<T> entity) throws Exception { List<T> result = new ArrayList<T>(); CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<T> query = builder.createQuery( entity ); Root<T> root = query.from( entity ); query.select( root ); //possible? query.orderBy(builder.asc(...)); result = em.createQuery( query ).getResultList(); return result; } Is there anyway for us to add orderby to the query and make it order by the primary key without specify the primary column as the expression? I mean, is there a

JPA 2 Criteria API: why is isNull being ignored when in conjunction with equal?

时间秒杀一切 提交于 2019-12-06 06:13:10
I have the following entity class (ID inherited from PersistentObjectSupport class): @Entity public class AmbulanceDeactivation extends PersistentObjectSupport implements Serializable { private static final long serialVersionUID = 1L; @Temporal(TemporalType.DATE) @NotNull private Date beginDate; @Temporal(TemporalType.DATE) private Date endDate; @Size(max = 250) private String reason; @ManyToOne @NotNull private Ambulance ambulance; /* Get/set methods, etc. */ } If I do the following query using the Criteria API: CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<AmbulanceDeactivation

JPA 2.0 subselect / subquery in order by clause with criteria api

て烟熏妆下的殇ゞ 提交于 2019-12-06 05:51:47
问题 I want to use JPA 2.0 criteria api to build the order by clause with a subselect. I know that you can do that in plain SQL but can it be mapped with criteria api? Can someone please give a code example? Example: Order(name, address) // table1 OrderPriority(address, priority) // table2 priority by address select o from Order o order by (select p.priority from OrderPriority p where p.address = o.address) 回答1: Criteria API queries are converted to JPQL and apparently subqueries in the order by