How to join unrelated entities with the JPA Criteria API

First: Foreign key relationship are not only for navigating. They mainly serve to ensure that no spurious values are introduced in the relationship. They also may help the database for query optimization. I would advise you to reconsider that. Anyway, for creating a query that uses several unrelated entities, you need to put them as … Read more

JPA Criteria Query distinct

I got it. The problem was my CriteraQuery needed to be of type String. This works: CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<String> query = builder.createQuery(String.class); Root<RuleVar> ruleVariableRoot = query.from(RuleVar.class); query.select(ruleVariableRoot.get(RuleVar_.varType)).distinct(true);

Using the JPA Criteria API, can you do a fetch join that results in only one join?

Instead of root.join(…) you can use root.fetch(…) which returns Fetch<> object. Fetch<> is descendant of Join<> but it can be used in similar manner. You just need to cast Fetch<> to Join<> it should work for EclipseLink and Hibernate … Join<MyEntity, RelatedEntity> join = (Join<MyEntity, RelatedEntity>)root.fetch(“relatedEntity”); …

How to write Subqueries with In-Expressions in JPA 2.0, Criteria API?

Below is the pseudo-code for using sub-query using Criteria API. CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Object> criteriaQuery = criteriaBuilder.createQuery(); Root<EMPLOYEE> from = criteriaQuery.from(EMPLOYEE.class); Path<Object> path = from.get(“compare_field”); // field to map with sub-query from.fetch(“name”); from.fetch(“id”); CriteriaQuery<Object> select = criteriaQuery.select(from); Subquery<PROJECT> subquery = criteriaQuery.subquery(PROJECT.class); Root fromProject = subquery.from(PROJECT.class); subquery.select(fromProject.get(“requiredColumnName”)); // field to map with main-query subquery.where(criteriaBuilder.and(criteriaBuilder.equal(“name”,name_value),criteriaBuilder.equal(“id”,id_value))); select.where(criteriaBuilder.in(path).value(subquery)); … Read more

Select MAX timestamp with JPA2 Criteria API

Instead of max one should use CriteriaBuilder.greatest for Timestamp (and for Date, String and other Comparables as well). And if you ever need MIN for Timestamp, then use least method instead Similar kind of issue can be be faced with less/greater/equal comparisons. Firs one accepts argument that extends Number, second one is for other comparables: … Read more

JPA – Criteria API and EmbeddedId

You need to use path navigation to access the attribute(s) of the Embeddable. Here is an example from the JPA 2.0 specification (using the static metamodel): 6.5.5 Path Navigation … In the following example, ContactInfo is an embeddable class consisting of an address and set of phones. Phone is an entity. CriteriaQuery<Vendor> q = cb.createQuery(Vendor.class); … Read more

Distinct results from Spring Data JPA Specification that uses join

Use the query parameter in your toPredicate method to invoke the distinct method. Sample below: public Predicate toPredicate(Root<Contact> root, CriteriaQuery<?> query, CriteriaBuilder cb) { final Predicate appPredicate = root.join(Contact_.managedApplications) .get(ManagedApplication_.managedApplicationId).in(appIds); query.distinct(true); …

JPA Criteria API with multiple parameters

Concept is to construct array of javax.persistence.Predicate which contains only predicates we want to use: Example entity to be queried: @Entity public class A { @Id private Long id; String someAttribute; String someOtherAttribute; … } Query itself: //some parameters to your method String param1 = “1”; String paramNull = null; CriteriaBuilder qb = em.getCriteriaBuilder(); CriteriaQuery … Read more

Spring Data JPA: Creating Specification Query Fetch Joins

Specification class: public class MatchAllWithSymbol extends Specification<Gene> { private String symbol; public CustomSpec (String symbol) { this.symbol = symbol; } @Override public Predicate toPredicate(Root<Gene> root, CriteriaQuery<?> query, CriteriaBuilder cb) { //This part allow to use this specification in pageable queries //but you must be aware that the results will be paged in //application memory! Class … Read more

techhipbettruvabetnorabahisbahis forumu