I am developing Web Application in Spring and Hibernate in my eclipse. I want to store my Hibernate queries or Sql queries in properties file and later use it in DAO. Please let me know how to do this? I am using generic DAO , is there any way to make generic queries? Later i would like to store other things like pages links,title also in properties file.
So basically three interrelated questions.
Since I could not think of any way to make generic queries So i have overridden findAll() method in concrete class.
I am using
<context:property-placeholder location="classpath:properties/database.properties"/>
to load the properties.But the problem is how to use it to get hql in java code or class files?
DAO
public abstract class AbstractHibernateDAO<T extends Serializable> {
public Class<T> clazz;//class object reference
protected SessionFactory mysessionFactory;
public void setClazz(final Class<T> clazzToSet) {
this.clazz = clazzToSet;
}
protected Session getCurrentSession() {
return mysessionFactory.getCurrentSession();
}
@SuppressWarnings("unchecked")
public List<T> findAll() {
return getCurrentSession().createQuery("from " + clazz.getName()).list();
}
My concerete DAO
@Repository("categDAO")
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS,value="request")
public class CategoryDAO extends AbstractHibernateDAO<category_pojo>{
public CategoryDAO() {
setClazz(category_pojo.class);
}
@Override
public category_pojo findOneByName(String name) {
return (category_pojo) getCurrentSession().createQuery("from "+clazz.getName()+" where categoryName=:name").setParameter("name",name).uniqueResult();
}
}
If I understood right, it sounds like regular Spring usage.
You may have a class like:
class userDao {
String findActiveUsers;
//... Getters and Setters
public List<User> findActiveUsers() {
return getCurrentSession().createQuery(findActiveUsers).list();
}
}
So, your application context would look like:
<bean id="userDao" class="my.package.UserDao">
<property name="findActiveUsers" value="FROM User u WHERE u.active=true"/>
</bean>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.