hibernate - when or why do i use Property.forName()? -
what 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 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"))
property.forname("propname")
returns matching property
instance.
having said this, means there no difference between first 2 code snippets posted in question. should use property.forname("propname")
when need use property multiple times in criteria or query. equivalent using direct no. (e.g. 11
) or using variable assigned no. (e.g. int x = 11
) , use variable everytime need use no.
for more details, see this.
now if talk 2nd question (3rd & 4th code snippets), working of both same. difference in api being used.
in 3rd code snippet, you're getting instance of property
, calling as()
method used generate alias particular property , returns instance of simpleprojection (subclass of projection)
.
while in 4th code snippet, you're getting instance of propertyprojection (subclass of projection)
doing projections.property("name")
.
so in both cases you're getting instance of projection
, you're adding projectionslist
. projectionlist having 2 overloaded methods called add()
. in 3rd code snippet you're calling add()
takes instance of projection
argument. in 4th code snippet you're calling version of add()
, takes instance of projection
first argument & alias property of projection
2nd argument. working of both same.
Comments
Post a Comment