如何使用通用的方式编写 Hibernate 查询(用于相同目的)?

huangapple go评论149阅读模式
英文:

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&lt;ImprovementArea&gt; getImprovementAreaList(MainPlanSheet planSheet) {

    return HibernateUtils.getSession(sessionFactory)
                    .createCriteria(ImprovementArea.class).add(Restrictions.eq(&quot;planSheet&quot;, planSheet))
                    .list();
      }

public List&lt;DevelopmentArea&gt; getDevlopmentAreaList(MainPlanSheet planSheet) {

    return HibernateUtils.getSession(sessionFactory)
	                 .createCriteria(DevelopmentArea.class).add(Restrictions.eq(&quot;planSheet&quot;, planSheet))
                    .list();
      }

public List&lt;SuggestedArea&gt; getSelectedSuggestedGoal(MainPlanSheet planSheet) {
	
	return HibernateUtils.getSession(sessionFactory)
			.createCriteria(SuggestedArea.class).add(Restrictions.eq(&quot;planSheet&quot;, 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&lt;OnBoardedEmployee&gt; getOnBoardedEmployees(Integer empId) {

    return HibernateUtils.getSession(sessionFactory)
            .createCriteria(OnBoardedEmployee.class)
            .add(Restrictions.eq(&quot;emId&quot;, 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 &lt;T&gt; List&lt;T&gt; getDevlopmentByEntity(MainPlanSheet planSheet, Class&lt;T&gt; entity) {
    return HibernateUtils.getSession(sessionFactory)
        .createCriteria(entity)
        .add(Restrictions.eq(&quot;planSheet&quot;, planSheet))
        .list();
}

And invoke it like:

List&lt;DevelopmentArea&gt; developmentAreas = getSelectedSuggestedGoal(planSheet, DevelopmentArea.class);

Edit: the last snippet can be rewritten as:

public &lt;T&gt; List&lt;T&gt; getDevlopmentByEntity(String param, Object value, Class&lt;T&gt; entity) {
    return HibernateUtils.getSession(sessionFactory)
        .createCriteria(entity)
        .add(Restrictions.eq(param, value))
        .list();
}

huangapple
  • 本文由 发表于 2020年7月24日 03:11:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63061535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定