Loading
Hibernate Spring integration

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  How many ways you can have Hibernate-Spring integration (Inversion of control for Hibernate) ?


Ans)

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

 



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

Hibernate Spring integration


Hibernate Spring integration

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free