5

I want to exploit the db independent HQL and Type safety of Criteria API. But I don't have entity classes.

Can I use Criteria API directly?

Sumeet Jindal
  • 882
  • 1
  • 7
  • 16
  • 1
    Question for answering this yourself: How would you achieve type safety without knowing the types of entities and entity attributes? – Thomas May 16 '12 at 09:29
  • By type safety I mean validating the queries at compile time. String queries cannot be checked at compile time. – Sumeet Jindal May 16 '12 at 09:35
  • We know that, but how would you validate a query without knowing whether you get the correct entity and whether the entity has the fields you access in the query? – Thomas May 16 '12 at 10:42
  • I agree entities have additional benefits. But I don't' know the entities during compilation. Still Criteria API is better than plain HQL – Sumeet Jindal May 16 '12 at 10:53

3 Answers3

4

No, you can't. The point of HQL and Criteria API is to query an object model, based on entities and associations between them.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I won't agree to this statement. ORM is only one of the features of HQL and Criteria API. Criteria API don't have much to do with ORM. – Sumeet Jindal May 16 '12 at 09:46
  • @SumeetJindal: So, when you're doing `criteria.createAlias("category.products", "category");`, how do you expect it to work if you don't have a Category entity with a toMany association to a Product entity? – JB Nizet May 16 '12 at 09:52
  • I am not concerned about associations. I want basic sql like operations – Sumeet Jindal May 16 '12 at 10:03
  • Criteria and HQL don't use SQL. They replace it with a language and API that works with entities. But if you think you know the answer to your own question, why do you ask it. Go and try using HQL without entities. – JB Nizet May 16 '12 at 10:07
2

First it's good to notice the Criteria API is commonly used as substitute of HQL for building dynamic queries ("HQL is extremely powerful, but some developers prefer to build queries dynamically using an object-oriented API, rather than building query strings" reference link).

Second, Hibernate is an Object/Relation(Database) persistence implementation. In order to use it we need to define the classes whose Objects shall be mapped into the targeted Database Tables(Entities).

For the Criteria API is obtained for an Hibernate Session using an Domain Class:

Criteria crit = sess.createCriteria(Cat.class);
crit.setMaxResults(50);
List cats = crit.list();

the Session object uses the mapping from the "Cat" class to its corresponding Table (probably named "Cat" in the Database).

So, finally without entities classes and their Mappings, no Session object . And no Session , no Criteria object.

Criteria needs to know the Classes (Cat.class) that need to be queried.

arthur
  • 3,245
  • 4
  • 25
  • 34
  • Can this help? http://docs.redhat.com/docs/en-US/JBoss_Enterprise_Web_Platform/5/html/Hibernate_Core_Reference_Guide/persistent-classes-dynamicmodels.html – Sumeet Jindal May 16 '12 at 09:44
  • the technic explained in the given link explained sounds great at the first time. But it has many drawbacks. First your code is not "type safe" and you can't check in compiled time whether the values (an their types) of the Map are really corresponding to the entities Table you wished to mapped. Secondly as a consequences you'll likely get in many some generic runtime Exceptions. and finally They says that the feature is currently experimental and subject to changes in the future. It's unstable for the moment, I'll give it a try – arthur May 16 '12 at 10:06
  • @Sumeet Jindal and a major problem is that, as already explained, the "Criteria" API need the .class of the Object to query. With dynamic Map as "entity mode", Criteria has no clue which class/entity will be selected, because the only give type available is "MAP". – arthur May 16 '12 at 10:16
0

how can you tell hibernate that which class you want to applied critera??

So you have to pass entity class name in createcriteria(Entity.class).

And ya... Criteria provides rich functionality, which will reduce your code.

But mind that... all things have positive - negative point.

Criteria has also some negative point...

So.. Be careful. Use as per your application related specification

Pradip Bhatt
  • 668
  • 9
  • 15