criteriaquery

Is there a way to reduce the amount of boiler-plate code associated with a CriteriaQuery (in JPA 2.0)?

狂风中的少年 提交于 2019-12-05 05:33:28
I love the type safety CriteriaQuery brings ing JPA 2.0 but it also brings a bit of boiler-plate code. For example, let say I have an entity called NamedEntity, which simply has an id and a String field called "name" (assume it has the unique constraint set to true). Here's what the NamedEntityManager might look like: public class NamedEntityManager { //inject using your framework EntityManager entityManager; //retrieve all existing entities of type NamedEntity from DB public Iterable<NamedEntity> queryAll() { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery

Polymorphic CriteriaQuery without inverse relationship

我只是一个虾纸丫 提交于 2019-12-03 05:21:50
I have the following EJB structure. Don't wonder about Animal and Inventory , these classes are only here to demonstrate the structure in a simplified way ( Update : I have revised the class names to construct a better understandable example. Another implementation of IdTag might be a BarcodeId ). Note that there is no inverse relationship from IdTag to Animal or Inventory , and let's assume the RfidTag.code is unique. I read Retrieving Polymorphic Hibernate Objects Using a Criteria Query and Hibernate polymorphic query but these discussions does not seem to answer my question. public

How do I write a JPA criteria query that matches a collection exactly?

陌路散爱 提交于 2019-12-01 08:12:12
I’m using JPA 2.0 with Hibernate 4.1.0.Final. I have a couple of classes, Groups and GroupMembers. Each GroupMember is tied to a user object @Entity @Table(name = "group") public class Group { @Id @NotNull @GeneratedValue(generator = "uuid-strategy") @Column(name = "ID") private String id; … @OneToMany(mappedBy = "group") private Set<GroupMember> members; @Entity @Table(name = "sb_msg_group_member") public class GroupMember { … @ManyToOne @JoinColumn(name = "USER_ID", nullable = false, updatable = true) private User user; Is it possible to write a JPA criteria query that given a java.util.Set

How do I write a JPA criteria query that matches a collection exactly?

一笑奈何 提交于 2019-12-01 05:31:16
问题 I’m using JPA 2.0 with Hibernate 4.1.0.Final. I have a couple of classes, Groups and GroupMembers. Each GroupMember is tied to a user object @Entity @Table(name = "group") public class Group { @Id @NotNull @GeneratedValue(generator = "uuid-strategy") @Column(name = "ID") private String id; … @OneToMany(mappedBy = "group") private Set<GroupMember> members; @Entity @Table(name = "sb_msg_group_member") public class GroupMember { … @ManyToOne @JoinColumn(name = "USER_ID", nullable = false,

How to check a collection size in JPA2

不打扰是莪最后的温柔 提交于 2019-11-30 20:20:44
Consider the following: @Entity public class Book { private List<String> authors; @ElementCollection public List<String> getAuthors() { return authors; } public void setAuthors(List<String> authors) { this.authors = authors; } } How to type a JPA2 CriteriaQuery expression which, say, will let me find all the Books which have more than 2 authors? JB Nizet In JPQL: select b from Book where size(b.authors) >= 2 Using the criteria API (but why would you replace such a simple static query with the following mess?): CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Book> criteriaQuery = cb

How to check a collection size in JPA2

时光毁灭记忆、已成空白 提交于 2019-11-30 04:09:07
问题 Consider the following: @Entity public class Book { private List<String> authors; @ElementCollection public List<String> getAuthors() { return authors; } public void setAuthors(List<String> authors) { this.authors = authors; } } How to type a JPA2 CriteriaQuery expression which, say, will let me find all the Books which have more than 2 authors? 回答1: In JPQL: select b from Book where size(b.authors) >= 2 Using the criteria API (but why would you replace such a simple static query with the

jpa 2 hibernate limit (max results) to a CriteriaQuery

元气小坏坏 提交于 2019-11-27 23:43:07
问题 maybe it's a silly question but I cannot find the answer in the docs: How can set a limit to the CriteriaQuery using JPA2? Thanks 回答1: A CriteriaQuery is not an executable Query. You need to create a TypedQuery first using EntityManager.createQuery(criteriaQuery) . You can then set the max results of this and execute it. 回答2: You could define the offset/limit like this: return em.createQuery(query) .setFirstResult(offset) // offset .setMaxResults(limit) // limit .getResultList(); 回答3: I

In JPA 2, using a CriteriaQuery, how to count results

ぃ、小莉子 提交于 2019-11-26 23:33:56
I am rather new to JPA 2 and it's CriteriaBuilder / CriteriaQuery API: CriteriaQuery javadoc CriteriaQuery in the Java EE 6 tutorial I would like to count the results of a CriteriaQuery without actually retrieving them. Is that possible, I did not find any such method, the only way would be to do this: CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<MyEntity> cq = cb .createQuery(MyEntityclass); // initialize predicates here return entityManager.createQuery(cq).getResultList().size(); And that can't be the proper way to do it... Is there a solution? Affe A query of type

JPA Query selecting only specific columns without using Criteria Query?

北城以北 提交于 2019-11-26 17:29:47
Is it possible to select, say, only properties A and B from an object using a JPA query without using criteria queries? To select all properties I'd just do something like: SELECT i FROM ObjectName i WHERE i.id = 10 But I have an object with many properties on a legacy system, and want to select just a few even though I'm aware selecting several properties is usually quick. Is this possible without using criteria queries? Thank you! Yes, like in plain sql you could specify what kind of properties you want to select: SELECT i.firstProperty, i.secondProperty FROM ObjectName i WHERE i.id=10

In JPA 2, using a CriteriaQuery, how to count results

为君一笑 提交于 2019-11-26 12:20:10
问题 I am rather new to JPA 2 and it\'s CriteriaBuilder / CriteriaQuery API: CriteriaQuery javadoc CriteriaQuery in the Java EE 6 tutorial I would like to count the results of a CriteriaQuery without actually retrieving them. Is that possible, I did not find any such method, the only way would be to do this: CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<MyEntity> cq = cb .createQuery(MyEntityclass); // initialize predicates here return entityManager.createQuery(cq)