英文:
How to make hibernate query( which are used for same purpose) generic?
问题
目前我正在使用Spring Boot + Hibernate进行工作,我大部分时间都在编写重复的Hibernate查询,这些查询每次执行的操作都是相同的,只不过实体类不同。
例如:在这里,我从三个不同的表improvement_area、development_area和suggested_area中检索列表。以下是相应的模型类。
public List<ImprovementArea> getImprovementAreaList(MainPlanSheet planSheet) {
return HibernateUtils.getSession(sessionFactory)
.createCriteria(ImprovementArea.class).add(Restrictions.eq("planSheet", planSheet))
.list();
}
public List<DevelopmentArea> getDevelopmentAreaList(MainPlanSheet planSheet) {
return HibernateUtils.getSession(sessionFactory)
.createCriteria(DevelopmentArea.class).add(Restrictions.eq("planSheet", planSheet))
.list();
}
public List<SuggestedArea> getSelectedSuggestedGoal(MainPlanSheet planSheet) {
return HibernateUtils.getSession(sessionFactory)
.createCriteria(SuggestedArea.class).add(Restrictions.eq("planSheet", planSheet))
.list();
}
正如你所看到的,这三个查询都在执行相同的操作,从相应的模型类中检索数据。
有人能帮助我或者提供更好的方法来使它变得通用吗?
现在又添加了另一个接受整数作为参数的查询:
public List<OnBoardedEmployee> getOnBoardedEmployees(Integer empId) {
return HibernateUtils.getSession(sessionFactory)
.createCriteria(OnBoardedEmployee.class)
.add(Restrictions.eq("empId", empId))
.list();
}
在这种情况下,如何使其通用,而不是使用四个不同的查询?
英文:
Currently I am working on Spring Boot + Hibernate and I mostly end up writing repetitive hibernate query every time which are doing same kinda job, only difference is that the entity class different.
Example: here, I am retrieving a list from three different tables improvement_area, development_area and suggested_area. Below are the respective model class.
public List<ImprovementArea> getImprovementAreaList(MainPlanSheet planSheet) {
return HibernateUtils.getSession(sessionFactory)
.createCriteria(ImprovementArea.class).add(Restrictions.eq("planSheet", planSheet))
.list();
}
public List<DevelopmentArea> getDevlopmentAreaList(MainPlanSheet planSheet) {
return HibernateUtils.getSession(sessionFactory)
.createCriteria(DevelopmentArea.class).add(Restrictions.eq("planSheet", planSheet))
.list();
}
public List<SuggestedArea> getSelectedSuggestedGoal(MainPlanSheet planSheet) {
return HibernateUtils.getSession(sessionFactory)
.createCriteria(SuggestedArea.class).add(Restrictions.eq("planSheet", planSheet))
.list();
}
as you could see all the three query is doing the same job, retrieving data from the respective model class.
Can someone please help me or suggest a better approach to make it generic?
Now another query added which accept Integer as parameter:
public List<OnBoardedEmployee> getOnBoardedEmployees(Integer empId) {
return HibernateUtils.getSession(sessionFactory)
.createCriteria(OnBoardedEmployee.class)
.add(Restrictions.eq("emId", empId))
.list();
}
In this scenario how can I make one generic Instead of four different query?
答案1
得分: 3
你可以简单地将类作为参数传递:
public <T> List<T> getDevlopmentByEntity(MainPlanSheet planSheet, Class<T> entity) {
return HibernateUtils.getSession(sessionFactory)
.createCriteria(entity)
.add(Restrictions.eq("planSheet", planSheet))
.list();
}
并且可以这样调用它:
List<DevelopmentArea> developmentAreas = getSelectedSuggestedGoal(planSheet, DevelopmentArea.class);
编辑:最后一段代码片段可以重写为:
public <T> List<T> getDevlopmentByEntity(String param, Object value, Class<T> entity) {
return HibernateUtils.getSession(sessionFactory)
.createCriteria(entity)
.add(Restrictions.eq(param, value))
.list();
}
英文:
You can simply pass the class as an argument:
public <T> List<T> getDevlopmentByEntity(MainPlanSheet planSheet, Class<T> entity) {
return HibernateUtils.getSession(sessionFactory)
.createCriteria(entity)
.add(Restrictions.eq("planSheet", planSheet))
.list();
}
And invoke it like:
List<DevelopmentArea> developmentAreas = getSelectedSuggestedGoal(planSheet, DevelopmentArea.class);
Edit: the last snippet can be rewritten as:
public <T> List<T> getDevlopmentByEntity(String param, Object value, Class<T> entity) {
return HibernateUtils.getSession(sessionFactory)
.createCriteria(entity)
.add(Restrictions.eq(param, value))
.list();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论