Questions tagged [hibernate-criteria]

The Criteria interface of Hibernate ORM , represents a query against a particular persistent class. The interface provides access to the powerful mechanism of hibernate criteria API, that allows the programmatic creation of queries against the DB.

Hibernate provides an alternate way of HQL to manipulate objects and it is called Hibernate Criteria Query. The Hibernate Session interface provides createCriteria() method which can be used to create a Criteria object. This criteria object returns instances of the persistence object's class when the application executes a criteria query.

The primary advantage of Criteria Query over HQL and native SQL is that it allows OOP control over the queries and hence it is more dynamic compared to HQL. In order to make HQL dynamic String concatenation needs to be done, which is not considered as a good programming concept.

To learn more about Criteria Query and it's usage the official website of Hibernate is a very good resource.

1659 questions
4
votes
1 answer

JPA Criteria API join query One To Many with condition

I have 2 Table Enterprise ID Name 1 Alex Product ID Name Status EnterpriseId 7 Iphone12 ACTIVE 1 8 Iphone11 ACTIVE 1 6 Iphone13 DISABLE 1 The relationship is one to many (one Enterprise having many Product). I want to…
4
votes
1 answer

Why does this Hibernate Criteria Query fails with "No explicit selection and an implicit one cold not be determined"?

I have an entity called Bucket, and I'm trying to build a criteria query to determine whether there is a Bucket stored with the "Name" property equals to "Bucket_1". So basically it is an exists query. There is nothing special about the Bucket…
Edy Bourne
  • 5,679
  • 13
  • 53
  • 101
4
votes
1 answer

Deleting Entities @OneToMany @ManyToOne. Key is still referenced from table

I have two entities - User and Song: User.class: @Entity @Table(name = "users") public class User { @Id @Column(name = "user_id") @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name =…
hdbgage
  • 123
  • 2
  • 8
4
votes
1 answer

Generate Case Expression from SQL Query - Criteria Builder, Criteria Query

select (case when dob <= '2020-02-03' then 'child' when dob >= '2020-02-03' then 'adult' else 'teenage' end) as AgeGroup, count(*) FROM student GROUP BY AgeGroup Query Result:- AgeGroup | Count child…
Deepak
  • 163
  • 1
  • 9
4
votes
2 answers

How to make a criteria query with 3 OR Criterions properly?

I'd like to make a criteria query with 3 OR Criterions and I dont know what is the best way to do it. Currently I only have 2 or Restrictions. Criteria crit =…
Kévin_Bransard
  • 676
  • 2
  • 11
  • 23
4
votes
1 answer

Encountered array-valued parameter binding, but was expecting [java.lang.String (n/a)]

I need to generate a query dynamically based on parameter passed and need to join three tables, getting below exception while building query with EntityManager CriteriaBuilder, same code structure is working if I convert it to Criteria but I want…
4
votes
1 answer

HibernateException: Unable to access lob stream

I have a problem. I have a Spring Boot project using Hibernate and I can not query an OID in the database. @Lob @Column(name = "documento_oid") private byte[] documentoOid; Query: if (tupled) { return em.createQuery(getTupleQuery()) …
4
votes
1 answer

How to use CriteriaBuilder to call db function

I have a string that might eventually contain spaces. I would like to replace those spaces with a regex that matches against \t,\r,\n. After replacing I would like to call regexp_like (an oracle function) to match a field against this string. I know…
user2175783
  • 1,291
  • 1
  • 12
  • 28
4
votes
3 answers

Hibernate check equal of two unrelated column

I have sample entity as following class Book{ int id String name int sid } class Author { int id String name List books int mid } Its not good design, I know that just trying to explain a scenario. Now my questions is, Is…
LynAs
  • 6,407
  • 14
  • 48
  • 83
4
votes
1 answer

hibernate join two entity in different schema or session

I have multiple schemas having their each table. ANIMAL.pet @Entity @Table(schema = "ANIMAL", name = "pet") @JsonIgnoreProperties(ignoreUnknown = true) public class Pet { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) …
log
  • 51
  • 1
  • 3
4
votes
1 answer

Hibernate column "fulfillment_status" is of type fulfillment_status but expression is of type character varying

I am having an issue updating enum fields in my postgres database from enums in my java entity using hibernate leading to the error in the title. My database looks like this \dT fulfillment_status List of data types Schema | …
reayn3
  • 363
  • 5
  • 16
4
votes
1 answer

How to compare two column value with hibernate Restrictions

I am having trouble converting MySQL Query into appropriate Hibernate code. Specifically the following SQL select a.id, b.id as id, b.timestamp as timestamp, b.speed as speed from abc a join xyz b on b.abc_id = a.id left xyz b2 on (a.id = b2.abc_id…
Maulik Kakadiya
  • 1,467
  • 1
  • 21
  • 31
4
votes
2 answers

Hibernate Child Collection Limited When Using Left Join in Criteria

When using hibernate criteria just altering the join type affects the results of the child collections of the root domain class. For instance, having class Parent have a one-to-many relationship with class Child with the following data: Parent |…
4
votes
1 answer

Filter Criteria by attribute with @Convert

I have a simple class with a list of Strings and this list is converted to be one column in db with @Convert and now i'm trying to create a criteria based on type attrbute. @Entity(name = "my_table") public class MyTable implements Serializable { …
Padi
  • 711
  • 9
  • 18
4
votes
1 answer

Spring Data JPA: Creating Specification Subquery from different tables

I am trying to build a specifications to predicate subquery for the below query. Select u.* from User u where u.login in (select ur.role_id from userRoles ur where ur.role_Id = roleId). till now this is the part I have built public static…
Nagamani mandava
  • 169
  • 1
  • 1
  • 7