criteria-api

JPA 2 — Using @ElementCollection in CriteriaQuery

非 Y 不嫁゛ 提交于 2019-11-27 15:14:06
问题 @Entity public class Person { @ElementCollection private List<Location> locations; [...] } @Embeddable public class Location { private Integer dummy; private Date creationDate; [...] } Given the following structure, I'd like to perform the HQL or CriteriaQuery equivalent of the following SQL: SELECT l.* FROM Location l INNER JOIN Person p ON (p.id = l.person_id) WHERE p.id = ? AND l.creationDate > ? I want to get back a list of Locations that are associated with the given person and whose

How to make a CriteriaBuilder join with a custom “on” condition?

本小妞迷上赌 提交于 2019-11-27 14:32:48
问题 I want make a query where I join 2 tables, using the CriteriaBuilder. In MySQL the query I'm trying to make would look like this: SELECT * FROM order LEFT JOIN item ON order.id = item.order_id AND item.type_id = 1 I want to get all orders and if they have an item of type #1, I want to join with this item. However, if no item of type #1 is found, I still want to get the order. I can't figure out how to make this with the CriteriaBuilder. All I know how to make is: CriteriaBuilder cb = em

Need help creating JPA criteria query

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 14:11:56
问题 I'm building my first Java EE web application using Glassfish and JSF. I'm fairly new to the criteria query and I have a query I need to perform but the javaee6 tutorial seems a little thin on examples. Anyway, I'm having a hard time creating the query. Goal: I want to pull the company with the most documents stored. Companies have a OneToMany relationship with Documents. Documents has a ManyToOne relationship with several tables, the "usertype" column distinguishes them. MySQL query: SELECT

Compare Date entities in JPA Criteria API

纵饮孤独 提交于 2019-11-27 13:01:52
Using JPA 2 with EclipseLink implementation. I'm trying to build a dynamic query which should bring me some records persisted after a given date. CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Event> criteria = builder.createQuery(Event.class); Root<Event> root = criteria.from(Event.class); criteria.select(root); criteria.distinct(true); List<Predicate> predicates = new ArrayList<Predicate>(); //... if (dateLimit != null){ ParameterExpression<Date> param = builder.parameter(Date.class, "dateLimit"); predicates.add(builder.lessThanOrEqualTo(root.get("dateCreated"), param)); }

Spring Data JPA. How to get only a list of IDs from findAll() method

江枫思渺然 提交于 2019-11-27 12:50:47
问题 I have a very complicated model. Entity has a lot relationship and so on. I try to use Spring Data JPA and I prepared a repository. but when I invoke a method findAll() with specification for the object a have a performance issue because objects are very big. I know that because when I invoke a method like this: @Query(value = "select id, name from Customer ") List<Object[]> myFindCustomerIds(); I didn't have any problems with performance. But when I invoke List<Customer> findAll(); I had a

JPA Criteria API with multiple parameters

假如想象 提交于 2019-11-27 10:57:24
I need to make a search method that uses the JPA Criteria API with multiple parameters. Now the problem is that not every parameter is required. So some could be null, and they shouldn't be included in the query. I've tried this with the CriteriaBuilder but I couldn't see how to make it work. With the Hibernate Criteria API this is fairly easy. Just create the criteria and then add Restrictions. Criteria criteria = session.createCriteria(someClass.class); if(someClass.getName() != null) { criteria.add(Restrictions.like("name", someClass.getName()); } How could I achieve the same with JPA's

What to use: JPQL or Criteria API? [closed]

情到浓时终转凉″ 提交于 2019-11-27 09:14:05
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 7 months ago . My Java application is using JPA for object persistence. The business domain is very simple (just three classes are persistent, with 3-5 properties in each). Queries are simple as well. The question is which approach I should use: JPQL or Criteria API? 回答1: I'm pretty sure

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

核能气质少年 提交于 2019-11-27 08:37:25
Using JPA 2.0. It seems that by default (no explicit fetch), @OneToOne(fetch = FetchType.EAGER) fields are fetched in 1 + N queries, where N is the number of results containing an Entity that defines the relationship to a distinct related entity. Using the Criteria API, I might try to avoid that as follows: CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<MyEntity> query = builder.createQuery(MyEntity.class); Root<MyEntity> root = query.from(MyEntity.class); Join<MyEntity, RelatedEntity> join = root.join("relatedEntity"); root.fetch("relatedEntity"); query.select

NHibernate: Select item with entry in element bag

六眼飞鱼酱① 提交于 2019-11-27 08:36:41
问题 I have a class with property of list. public class Paperboy{ private int _id; private string _lastname; private string _firstname; private string _mobile; private string _phone; private IList<string> _additionalPhoneNumbers; } The List is mapped as bag with key and element. <class name="Paperboy" table="tblPaperboy" lazy="false"> <id name="_id" column="Id" access="field" > <generator class="assigned"/> </id> <property name ="_lastname" column="Lastname" access ="field" /> <property name ="

Subquery in From claus in JPA2 Criteria

限于喜欢 提交于 2019-11-27 08:21:10
问题 I have a data model where an account can have multiple users: class Account { Long id; } class User { Long id; @ManyToOne Account account; } I'd like to do the following query which displays the number of users of each account: select Account.id, NumUsers.num from Account, (select Account.id as account_id, count(User.id) as num from User join Account on User.account_id=Account.id group by Account.id) as NumUsers where Account.id=NumUsers.account_id; I know I can re-write this specific query