criteria-api

Need help creating JPA criteria query

泄露秘密 提交于 2019-11-28 21:34: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 USERID, COUNT(USERID) AS CNT FROM DOCUMENTS WHERE USERTYPE="COMPANY" GROUP BY USERID ORDER BY CNT DESC

Using Java generics for JPA findAll() query with WHERE clause

℡╲_俬逩灬. 提交于 2019-11-28 20:41:58
So, After a 10+ year break I'm coming back to Java and trying out stuff with JPA and Java generics. I've created a generics based findAll(other) JPA query that basically does SELECT * FROM source WHERE other_id = other.id; This is where I'm up to. It works, but I'm wondering if there's a better, cleaner way to do it. Using ManagedType was hard, and there's not much complete documentation or simple examples around. I've decided to keep my code as generic as possible (no pun intended) so I use JPA2. This is the root of all Entity Classes. I probably don't need it, but it stops me from having

JPA/Criteria API - Like & equal problem

只愿长相守 提交于 2019-11-28 16:58:27
I'm trying to use Criteria API in my new project: public List<Employee> findEmps(String name) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Employee> c = cb.createQuery(Employee.class); Root<Employee> emp = c.from(Employee.class); c.select(emp); c.distinct(emp); List<Predicate> criteria = new ArrayList<Predicate>(); if (name != null) { ParameterExpression<String> p = cb.parameter(String.class, "name"); criteria.add(cb.equal(emp.get("name"), p)); } /* ... */ if (criteria.size() == 0) { throw new RuntimeException("no criteria"); } else if (criteria.size() == 1) { c.where(criteria

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

家住魔仙堡 提交于 2019-11-28 15:30:47
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? Pascal Thivent I'm pretty sure this has already been covered here on SO but I couldn't find the existing question. So, here is my point of view on the question: I find JPQL queries easier to write/read. I find the Criteria API nice for building dynamic queries. Which is basically what you'll find in Hibernate: Criteria vs. HQL . But there is one

Subquery in From claus in JPA2 Criteria

自作多情 提交于 2019-11-28 14:08:51
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 as: select Account.id, count(User.id) from Account join User on User.account_id=Account.id group by

How to properly cast string to number with JPA2 Criteria API?

北慕城南 提交于 2019-11-28 13:57:10
I am trying to write a query with subselect where a string is cast to a long. I'm probably missing something? Query looks like: CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Task> query = cb.createQuery(Task.class); Root<Task> from = query.from(Task.class); Subquery<Long> subquery = query.subquery(Long.class); Root<EntityKeyword> fromKeyword = subquery.from(EntityKeyword.class); subquery.select(fromKeyword.get(EntityKeyword_.relatedToId).as(Long.class)); subquery.where(cb.like(fromKeyword.get(EntityKeyword_.keyword), term)); query.where(cb.in(from.get(ModelEntity_.id)).value

JPA Criteria select all instances with max values in their groups

微笑、不失礼 提交于 2019-11-28 10:24:18
Is there a way to write with JPA 2 CriteriaBuilder the equivalent of the following query? select * from season s1 where end = ( select max(end) from season s2 where s1.contest_id=s2.contest_id ); In JPQL this query is: Select s1 from Season s1 where s1.end = ( select max(s2.end) from Season s2 where s1.contest=s2.contest ) This should work, with contest being either a basic Integer property, or a ManyToOne property pointing to another non-basic Entity. EntityManger em; //to be injected or constructed CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Season> cq = cb.createQuery(Season

JPA Criteria API: Multiple condition on LEFT JOIN

拜拜、爱过 提交于 2019-11-28 10:20:27
I have a join reference like following for which the first join expression is constructed by the JPA API automatically. CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> c = cb.createTupleQuery(); Root<Demand> demands = c.from(Demand.class); Join<Demand, Metadata> joinMetadata = demands.join("metadatas", JoinType.LEFT); but, I would like to add an aditional condition to my joinMetadata like Metadata.type="AFFECTATION_KEY" but I don't know how. Many thanks for any help James JPA always joins by the mapping join columns, only. JPA 2.0 does not support an ON clause,

How to find by date from timestamp column in JPA criteria?

删除回忆录丶 提交于 2019-11-28 09:15:38
问题 I want to find a record by date. In entity and database table, datatype is timestamp. I used Oracle database. @Entity public class Request implements Serializable { @Id private String id; @Version private long version; @Temporal(TemporalType.TIMESTAMP) @Column(name = "CREATION_DATE") private Date creationDate; public Request() { } public Request(String id, Date creationDate) { setId(id); setCreationDate(creationDate); } public String getId() { return id; } public void setId(String id) { this

How to write this linq query with Criteria or QueryOver API

﹥>﹥吖頭↗ 提交于 2019-11-28 05:37:28
问题 Is it possible to convert this code at below, written by using Query(linq) api to Criteria or QueryOver API in NHibernate? I'm using this to format data into DTO's also it works with just one round-trip to db. Note: I tried transformers.aliastobean but I can only use one transformer at a time. Is it possible to use multiple transformer in one query? from entityType in Provider.GetSession().Query<crmEntityType>() .Fetch(x => x.Association) .Fetch(x => x.Fields) .AsEnumerable() where