Use of Handling associations and collections in SQL by R4R Team

We have already discussed is that in the hibernate every thing is possible which we want to do. So if It is possible to eagerly join in the Person to avoid the possible extra roundtrip for initializing the proxy. This is done via the addJoin() method, which allows you to join in an association or collection.

sess.createSQLQuery("SELECT c.ID, NAME, BIRTHDATE, PERSON_ID, D_ID, D_NAME FROM STUDENTS c, PERSONS d WHERE c.PERSON_ID = d.D_ID")
 .addEntity("student", Student.class)
 .addJoin("student.person");

With the help of the above code of the example the returned Student's will have their dog property fully initialized without any extra roundtrip to the database. But we have to remember that added an alias name ("student") to be able to specify the target property path of the join. It is possible to do the same eager joining for collections, e.g. if the Student had a one-to-many to Person instead.

sess.createSQLQuery("SELECT ID, NAME, BIRTHDATE, D_ID, D_NAME, STUDENT_ID FROM STUDENTS c, PERSONS d WHERE c.ID = d.STUDENT_ID")
 .addEntity("student", Student.class)
 .addJoin("student.persons");

We reaching the limits of what is possible with native queries, without starting to enhance the sql queries to make them usable in Hibernate. Problems can arise when returning multiple entities of the same type or when the default alias/column names are not enough.
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!