Hibernate Template Session query
We are using Spring HibernateTemplate class for hibernate operation. I need to execute a query with in clause and setting the options for in using an arrayList. For that i need a org.hibernate.Query object.
String queryStr = "from CertMetadataCfg where metadataId in (:metadataIds)";
Query query=hibernateTemplate.getSessionFactory().openSession().createQuery(queryStr)
query.setParameterList("metadataIds", metadataSet);
but for retrieving Query object we have to open a new session. Why do we need to open a session. and when should we close thi开发者_JAVA技巧s session .
HibernateTemplate does the opening and closing of the session handling.
Please check the documentation.
Use findByNamedParam -
getHibernateTemplate().findByNamedParam("from CertMetadataCfg where metadataId in (:metadataIds)", "metadataIds", metadataIds);
If you want to do only a read, you dont need to open the session. Use getHibernateTemplate().find(queryString);
精彩评论