Reputation:
in hibernate to create criteria we use
Criteria criterea=session.createCritera(SomeClass.class)
It may be available in some other examples too but I am not able to understand the structure of these type of methods.
NOTE this is an example I am trying to put to understand use of SomeClass.class
like arguments
my question here is what is purpose of SomeClass.class
? why do we need it, what is the advantages of using it as argument.
Edit its not a duplicate but have string connection to this question
Upvotes: 2
Views: 118
Reputation: 1
Hibernate provides many ways to handle the objects in relation with RDBMS tables. One way is Session interface providing createCriteria() method which can be used to create a Criteria object. As name says criteria, it is useful to execute queries by applying filtration rules and logical conditions of programmer wish.
For example:
Criteria obj=session.createCritera(Galaxy.class) // say SomeClass is Galaxy.class
List results = obj.list();
Here, criteria query is one which will simply return every object that corresponds to the Galaxy class.
We even can restrict the results with criteria, example add() method available for Criteria object to add restriction for a criteria query. Following is the restriction to return the records with planet which has 7.4 billion population from Galaxy class:
Criteria cr = session.createCriteria(Galaxy.class);
cr.add(Restrictions.eq(“planet”, 75000000000));
List results = cr.list();
Upvotes: -1
Reputation: 273860
.class
syntax?If you attach .class
to the end of a class name, you get the Class<T>
object corresponding to the class.
Examples:
String.class
returns an instance of Class<String>
Integer.class
returns an instance of Class<Integer>
Reflection! If you have access to a class object, you can do all kinds of cool stuff! You can call methods, get and set values of fields...
I haven't used hibernate before, but this syntax is used in other libraries as well, especially in ORMs or JSON serializers. I'll use JSON serializers as an example as I'm more familiar with those.
In a JSON serializer, you need to give it a class object because it needs to get all the fields that you want to serialize to JSON. It uses reflection to get and set the values of those fields, then convert them to JSON. When it deserializes JSON, it finds the field that needs to be set with the name in the JSON. These operations require a Class
object because without it, how can Java know which class should it find the field? Also, to create a new object with reflection, a Class
is needed as well!
Upvotes: 2