when or why do i use Property.forName()?
hibernate, hibernate-criteria
Solution
`Property.forName("propName")` always returns you the matching `Property` instance.
Having said this, means there is no difference between the first two code snippets posted in your question. You should use `Property.forName("propName")` when you need to use that property multiple times in Criteria or Query. It is equivalent to using direct no. (`e.g. 11`) or using variable assigned to the no. (`e.g. int x = 11`) and use the variable everytime you need to use the no.
For more details, see this.
Now if I talk about the 2nd question (3rd & 4th code snippets), working of both is the same. The only difference is in the API being used.
In 3rd code snippet, you're getting the instance of `Property`, calling its `as()` method which is used to generate alias for that particular property and returns an instance of `SimpleProjection (subclass of Projection)`.
While in 4th code snippet, you're getting instance of `PropertyProjection (subclass of Projection)` by doing `Projections.property("Name")`.
So in both cases you're getting instance of `Projection`, which you're adding to `ProjectionsList`. Now ProjectionList is having 2 overloaded methods called `add()`. In 3rd code snippet you're calling `add()` that takes only instance of `Projection` as argument. In 4th code snippet you're calling another version of `add()`, that takes instance of `Projection` as first argument & `alias for the property of Projection` as the 2nd argument. So ultimately working of both is the same.
Problem
What is the difference between : ``` List cats = session.createCriteria(Cat.class) .add( Restrictions.like("name", "F%") .list(); ``` and ``` List cats = session.createCriteria(Cat.class) .add( Property.forName("name").like("F%") ) .list(); ``` Or for that matter, difference between : ``` Criteria cr = session.createCriteria(User.class) .setProjection(Projections.projectionList() .add(Property.forName("id").as("id")) .add(Property.forName("name").as("name")) ``` and ``` Criteria cr = session.createCriteria(User.class) .setProjection(Projections.projectionList() .add(Projections.property("id"), "id") .add(Projections.property("Name"), "Name")) ```