-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Session.get with FetchMode
PostPosted: Thu Jul 31, 2008 7:38 am 
Newbie

Joined: Mon Dec 05, 2005 8:00 am
Posts: 5
Location: Rome
Is possible to execute something like:

session.get(myObjId, FETCH_JOIN_ALL) ?

I was trying something like

Criteria criteria = session.createCriteria(myClass);

// for each collection property
criteria.setFetchMode(property, FetchMode_JOIN);

what I miss now is how can I set the identifing property without knowing the property name....as I already do with session.get()

I've found I can ask the Persister, but I cannot access the persister from session interface...

I think it would be useful to have something like the session.get method I suggested or something like

session.get(myObjId, StringArrayOfPropertiesYouWantToFetch)

I can understand why this method is not in the hibernate core, but I wish to add this on our framework....

Hibernate version: 3.2.4sp1


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 31, 2008 4:39 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
I think you'll find that there are lots of classes and methods in the Hibernate Criteria API that allow you to do just that.

The Example object is great. Rather than they 'array of properties' you just initialize properties of an example instance of the POJO you're after, and create a Criterion out of it. It's so easy!


How to Use the Hibernate Criteria API: A Simple Tutorial

Code:
public static void main(String[] args) {
  User user = new User();
  user.setEmailAddress(".com");
  Example example = Example.create(user);
  example.enableLike(MatchMode.END);
  Session session = HibernateUtil.beginTransaction();
  Criteria criteria = session.createCriteria(User.class);
  criteria.add(example);  List results = criteria.list();
  for (int i = 0; i<results.size(); i++) {
    System.out.println(results.get(i).toString());
  }
  HibernateUtil.commitTransaction();
}


Code:
public static void main(String args[]) {
  Session session = HibernateUtil.beginTransaction();
  Criterion c1 = Restrictions.gt("id", (long)2);
  Criterion c2 = Restrictions.lt("id", (long)8);
  Criterion c3 = Restrictions.isNotNull("emailAddress");
  User user = new User();
  user.setEmailAddress(".com");
  Example c4 = Example.create(user);
  c4.enableLike(MatchMode.END);
  c4.ignoreCase();
  Criteria criteria = session.createCriteria(User.class);
  criteria.add(c1);
  criteria.add(c2);
  criteria.add(c3);
  criteria.add(c4);
  List results = criteria.list();
  HibernateUtil.commitTransaction();
  for (int i = 0; i<results.size(); i++) {
    System.out.println(results.get(i).toString());
  }
}

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.