Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to do Join in Hibernate using Criteria

843830Sep 19 2007 — edited Sep 20 2007
Hi
I need help for select operation using criteria in hibernate and I dont want to use HQL.
I have two table called List( fields are list_id,list_name,list_desc etc) and List Details(fields are list_id,product_id)
this is my list class:
private long listid;

private String listname;

private String listdesc;


private Set listDetails =new HashSet();
And setters and getters for the above fields.
......

this is my listdetails class:
private long id;
private long listid;
private String prodid;

and setters and getters for the above fields.
========================================
This is how I am trying to fetch:
Session session = null;

try {
// Create the SessionFactory
System.out.println("hi");
sessionFactory = new Configuration().configure().buildSessionFactory();

System.out.println(sessionFactory);

session =sessionFactory.openSession();

long listid=listCriteriaDVO.getListid();

System.out.println("listid::::::"+listid);


Criteria criteria=session.createCriteria(com.audiblecommunity.lists.entity.List.class);

criteria.add(Restrictions.eq("listid", new Long(listid))).setFetchMode("listDetails", FetchMode.JOIN);

ArrayList lst=(ArrayList)criteria.list();

com.myapp.List lst1=(com.myapp.List lst.get(0);

System.out.println("list id heree"+lst1.getListid());

System.out.println("coll size"+lst1.getListDetails().size());

} catch (Exception e) {
//log.error("Initial SessionFactory creation failed.", ex);
e.printStackTrace();
System.out.println(e.getMessage());
//throw new ExceptionInInitializerError(ex);
}

==================================================
I am able to fetch the list id from the parent table but What I need is the collection(listDetails in this case which is defined as set) conatining all the child records those are in ListDetails table.But its not returning anything in the collection.I dont know whats wrong with this code.Can somebody please help.
I referred this article:
http://www.devarticles.com/c/a/Java/Hibernate-Understanding-Associations/3/
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 18 2007
Added on Sep 19 2007
4 comments
278 views