Questions tagged [criteria]

Mechanism typical to ORM frameworks that allows the creation of queries against a database in a dynamic and object-oriented fashion. When writing a query using Criteria one uses an API instead of writing a query using a defined language (SQL).

Mechanism typical to ORM frameworks that allows the creation of queries against a database in a dynamic and object-oriented fashion. When writing a query using Criteria one uses an API instead of writing a query using a defined language (SQL).

All the most known ORM frameworks provide Criteria APIs, like Hibernate Criteria and after the release of JPA it was standardized as JPA Criteria.

2829 questions
37
votes
7 answers

How to use hibernate criteria to return only one element of an object instead the entire object?

I'm trying to get only the list of id of object bob for example instead of the list of bob. It's ok with a HQL request, but I would know if it's possible using criteria ? An example : final StringBuilder hql = new StringBuilder(); hql.append(…
PennyLane
33
votes
8 answers

Get record with max id, using Hibernate Criteria

Using Hibernate's Criteria API, I want to select the record within a table with the maximum value for a given column. I tried to use Projections, creating an alias for max(colunName), then using it in restrictions.eq(), but it keeps telling me…
Amr Faisal
  • 2,004
  • 6
  • 27
  • 36
32
votes
3 answers

How to paginate a JPA Query

I have a submission table with columns like ID, Name, Code among other properties. My requirement is to search for records based on the mentioned properties and return a paginated set. This is the pseudocode for what I am looking…
Tapan Nallan
  • 1,762
  • 3
  • 17
  • 37
30
votes
7 answers

JPA - FindByExample

Does anyone have a good example for how to do a findByExample in JPA that will work within a generic DAO via reflection for any entity type? I know I can do it via my provider (Hibernate), but I don't want to break with neutrality... Seems like…
Dave
  • 21,524
  • 28
  • 141
  • 221
29
votes
2 answers

Group by month with criteria in Hibernate

I'm trying to get a report using Criteria and ProjectionList, and I'm pretty new using this through hibernate. So I have this model: private Long _userId; private Category _category; private Long _companyId; private Double _amount; private…
RoD
  • 691
  • 1
  • 6
  • 8
28
votes
3 answers

How to return an entity with chosen columns using Criteria

I'm really new with Hibernate. I want a List using hibernate criteria, but only with fields User id and name filled up. Is that possible? Something like the query shown below: SELECT user.id, user.name FROM user Regards.
Rodrigo Zero
28
votes
3 answers

How to get the resultant array from a Mongoid::Criteria without an "each" block

Our application uses ajax very heavily and as a result of this we have statements like var items = #{@items.to_json} in all our views. Now @items is being set in the controller as @items=Item.all. The problem is that @items is a Mongoid::Criteria…
Khaja Minhajuddin
  • 6,653
  • 7
  • 45
  • 47
28
votes
8 answers

How do add NOLOCK with nHibernate?

How do you add NOLOCK when using nhibernate? (criteria query)
mrblah
  • 99,669
  • 140
  • 310
  • 420
27
votes
3 answers

NHibernate - CreateCriteria vs CreateAlias

Assuming the following scenario: class Project{ public Job Job; } class Job{ public Name; } Assuming I want to use the Criteria API to search for all projects whose Job has the name "sumthing". I could use the CreateAlias to create an alias…
Megacan
  • 2,510
  • 3
  • 20
  • 31
27
votes
3 answers

JPA CriteriaBuilder Subquery multiselect

I have a question about Subquery class in jpa. I need to create subquery with two custom field, but subquery doesn't have multiselect method and select method has Expression input parameter(In query this is Selection) and constact method not…
grinder
  • 436
  • 1
  • 6
  • 10
26
votes
4 answers

jpa criteria for many to many relationship

I have 2 POJO classes in Java, Answer and Collaborator, in a many-to-many relationship. class Answer { @ManyToMany(cascade = CascadeType.ALL) @JoinTable(name = "ANSWERS_COLLABORATORS", joinColumns = { @JoinColumn(name = "aid") },…
user998692
  • 5,172
  • 7
  • 40
  • 63
26
votes
4 answers

Hibernate Criteria for Dates

In oracle I have dates in format 17-April-2011 19:20:23.707000000 I would like to retrieve all orders for 17-04-2011. SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-YYYY"); String myDate = "17-04-2011"; Date date =…
danny.lesnik
  • 18,479
  • 29
  • 135
  • 200
26
votes
1 answer

hibernate criteria with exists clause

I cannot find a solution to a problem that seems to be easy. Say there are 2 entity classes: class A { Set bs; } class B { String text; } How to create a criteria query that returns all A's that contains at least one B entity which…
meliniak
  • 762
  • 1
  • 9
  • 16
25
votes
2 answers

Combining conditional expressions with "AND" and "OR" predicates using the JPA criteria API

I need to adapt the following code example. I've got a MySQL query, which looks like this (2015-05-04 and 2015-05-06 are dynamic and symbolize a time range) SELECT * FROM cars c WHERE c.id NOT IN ( SELECT fkCarId FROM bookings WHERE (fromDate…
Steven X
  • 394
  • 1
  • 3
  • 14
24
votes
1 answer

Using Hibernate's Criteria and Projections to Select Multiple Distinct Columns

Using Hibernate's Criteria, I want to execute the equivalent of: select distinct uspscity, state from citycomplete where USPSCITY = 'HOUSTON' I thought doing the following would yield the results I wanted: ProjectionList projList = new…
bvulaj
  • 5,023
  • 5
  • 31
  • 45