Reputation: 1
I am new to Spring, Hibernate environment. I have two tables t1, t2 also having two POJO objects for t1 and t2. I need to retrive all the data from two tables using Hibernate Template. Please anyone give answer. Thanks in Advance.
Upvotes: 0
Views: 5200
Reputation: 6158
Call this one:
protected List findAll(Class clazz) {
return getHibernateTemplate().find("from " + clazz.getName());
}
This function will return you a list of object.
updated:
String hql="SELECT T.* FROM TEST T, STUDENT S WHERE S.DNO=? AND S.CLASS_ID=T.CLS_ID AND T.STATUS='A' AND T.BATCH=S.BATCH AND T.SHIFT=S.SHIFT";
List list = getHibernateTemplate.find(hql,"condition");
Upvotes: 1
Reputation: 9697
Assuming you have the hbm files/annotated pojo and Spring properly configured, you could file an SQL query using session.createSQLQuery(sqlQuery) for selecting data from both the tables(If you want this at one shot) . You would get an Object[] in the result which you could use . Else you could fire separate queries as subodh recommends.
check here for a reference.
return this.getHibernateTemplate().execute(new HibernateCallback<List>() {
@Override
public List doInHibernate(Session session) throws HibernateException, SQLException {
String sqlQuery = "yourQueryHere";
SQLQuery query = session.createSQLQuery(sqlQuery);
List lstData = query.list();
for (Object row : lstData) {
Object[] recordArray = (Object[]) row;
//recordArray[1] , recordArray[2] will give the columns
}
}
});
or instead of callback you could use
this.getHibernateTemplate().getSessionFactory().getCurrentSession()
to get the session object . Note as of Spring 3.0 using hibernate template is not recommended. You can just inject the sessionFactory, get the session from there and use it .
Upvotes: 1