Associations in Criteria Query by R4R Team

With the help of the navigating associations using createCriteria() it can be specify constraints upon related entities: It is divided into two parts which is given below:


Like as (1):


List student = session.createCriteria(Student.class)

    .add( Restrictions.like("name", "G%") )

    .createCriteria("Porkar")

        .add( Restrictions.like("name", "G%") )

    .list();


(2) createCriteria() returns a new instance of Criteria that refers to the elements of the kittens collection. There is also an alternate form that is useful in certain circumstances which is shown below:


List student = session.createCriteria(Student.class)

    .createAlias("Poraskar", "pkr")

    .createAlias("romit", "rm")

    .add( Restrictions.eqProperty("pkr.name", "rm.name") )

    .list();


we need to remember is that (createAlias() does not create a new instance of Criteria.) 


The praskar collections held by the Student instances returned by the previous two queries are not pre-filtered by the criteria. If need to retrieve just the poraskar that match the criteria, so we use a ResultTransformer.


List student = session.createCriteria(Student.class)

    .createCriteria("proaskar", "pkr")

        .add( Restrictions.eq("name", "G%") )

    .setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP)

    .list();

Iterator iter = student.iterator();

while ( iter.hasNext() ) {

    Map map = (Map) iter.next();

    Student student = (Student) map.get(Criteria.ROOT_ALIAS);

    Student poraskar = (Student) map.get("kt");

}

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!