Refresh Your Java - Before Java Interview
We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....
Could be done in Two ways.
i)By using
"HibernateTemplate"
ii) By
using "HibernateDaoSupport" .
With HibernateTemplate and Callback
method:
public class YourDaoImpl implements
YourDao {
private
SessionFactory sessionFactory;
public void
setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Collection loadProductsByCategory(final String category)
throws DataAccessException {
HibernateTemplate ht = new
HibernateTemplate(this.sessionFactory);
return (Collection) ht.execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws
HibernateException {
Query query = session.createQuery(
"from test.Product product where
product.category=?");
query.setString(0, category);
return query.list();
}
});
}
}
By uisng HibernateDaoSupport :
public class YourDaoImpl extends HibernateDaoSupport implements
YourDao {
public Collection
loadProductsByCategory(String category)
throws DataAccessException, MyException {
Session session = getSession(getSessionFactory(), false);
try {
List result = session.find(
"from test.Product product where
product.category=?",
category, Hibernate.STRING);
if (result == null) {
throw new MyException("invalid search
result");
}
return result;
} catch (HibernateException ex) {
throw convertHibernateAccessException(ex);
}
}
}
Similarly you also have the following
i) "JdoTemplate and
JdoDaoSupport"
ii) TopLinkTemplate and
TopLinkDaoSupport
iii) n iBATIS API
SqlMapClientTemplate and SqlMapClientDaoSupport
This Portal is intended to put all Java/J2ee related topics at one single place for quick referance, not only Technical , but also the Project Management Related thing such as Development Process methodoogies build process, unit testing etc.,
This Portal has More than 500 Java Interview Questions (also could be Considered as Quick Notes) very neatly separated topic by topic with simple diagrams which makes you easily understandable. Importantly these are from our Realtime expericance.