简体   繁体   English

在通用Spring CRUD DAO中获取正确的Hibernate模板

[英]Getting the correct Hibernate template in a generic Spring CRUD DAO

I'm still quite new to Spring, and I've found it to irritating making all these CRUD DAOs, so I've made a "public class GenericCRUDDAO extends HibernateDaoSupport implements CRUDDAO". 我对Spring还是很陌生,并且发现制作所有这些CRUD DAO令人不快,因此我制作了一个“公共类GenericCRUDDAO扩展了HibernateDaoSupport实现CRUDDAO”。 In my service objects I then simply say something like 然后,在服务对象中,我只说类似

private GenericCRUDDAO<User, Integer> userDAO = new GenericCRUDDAO<User, Integer>();

and no more do I have to write simple DAOs and wire them up. 而且,我无需再编写简单的DAO并将其连接起来。 Yay! 好极了! Except for one thing I'm sure all you experienced Spring developers see right away: I cannot get the Hibernate template inside the GenericCRUDDAO, so doing 除了一件事情,我确定所有经验丰富的Spring开发人员都会立即看到:我无法在GenericCRUDDAO中获取Hibernate模板,因此

HibernateTemplate ht = getHibernateTemplate();

gives me a ht that is null. 给我的ht为空。 Not so good. 不太好。 I thought of wiring it in, meaning making a genericCRUDDAO bean and then setting a static AnnotationSessionFactoryBean, but that still wouldn't give me a HibernateTemplate. 我考虑过将其连接起来,这意味着要制作一个通用的CRUDDAO bean,然后设置一个静态的AnnotationSessionFactoryBean,但这仍然不能给我HibernateTemplate。 Any suggestions on how I work around that so that I can have my Hibernate Template? 关于如何解决该问题的任何建议,以便可以使用我的Hibernate模板?

Any more issues with making a generic CRUD DAO I should be thinking about? 我应该考虑制作通用CRUD DAO的其他问题吗?

Cheers 干杯

Nik

For many, HibernateTemplate and HibernateDaoSupport are on the outs, and instead injecting a SessionFactory is preferred. 对于许多人来说, HibernateTemplateHibernateDaoSupport市场上,而是首选注入SessionFactory Not everyone, mind you, but it is a trend, which I adopted not too long ago, removing HibernateTemplate from my own generic DAO. 请注意,不是所有人,但这是一种趋势,我不久前就采用了这种趋势,即从我自己的通用DAO中删除了HibernateTemplate

This blog has a pretty good summary. 这个博客有一个很好的总结。

The author's examples should be able to help you get to where you want to be. 作者的例子应该能够帮助您到达想要的地方。

GenericDao GenericDao

Well, to me, if your GenericDAO is 'generic', then you might need only one instance , and do all with that single instance. 好吧,对我来说, 如果您的GenericDAO是'generic',那么您可能只需要一个实例 ,并使用该单个实例完成所有操作。

I'm sure it doesn't bother you to wire on instance, it's at the repetition that you get mad (and I agree with you). 我确信它不会打扰您,例如,反复发脾气(我也同意)。

For example, you could pass the Entity class to a generic method . 例如, 您可以将Entity类传递给通用方法

  • public void save(Class, E...) : lets you save one or more instances of E type, E being one of your entities. public void save(Class,E ...):可以保存一个或多个E类型的实例,E是您的实体之一。
  • public E load(Class, Long id) : loads an entity. public E load(Class,Long id):加载一个实体。
  • ... ...

     /** Assuming the entities have a superclass SuperEntity with getIdent(). */ public class GenericDaoImpl implements GenericDao { /** Save a bunch of entities */ public void save(SuperEntity... entities) { for(SuperEntity entity : entities) { getSession().save(entity); } } /** Load any entity. */ public <E extends SuperEntity> E load(Class<E> entityClass, Long ident) { return (E)getSession().load(entityClass, ident); } // other generic methods } 

Variant 变种

In our applications, we actually have a variant for this. 在我们的应用程序中,我们实际上为此有一个变体。 Because we have many specific request for each Dao, we need the specific Dao classes anyway (create the class and wire it), so to avoid the special case of a Dao that would not be defined, we make the specific Dao classes right away. 因为每个Dao都有许多特定的请求,所以无论如何我们都需要特定的Dao类(创建并连接它),因此为了避免无法定义的Dao的特殊情况,我们立即制作特定的Dao类。

Coding 编码

But in no way we repeat the code. 但是我们绝不会重复代码。 All our Daos extends the GenericDao, providing in the constructor the Class parameter that is needed. 我们所有的Daos扩展了GenericDao,在构造函数中提供了所需的Class参数。 Sample code (not complete, simple to get the basic idea): 示例代码(不完整,很容易获得基本思想):

    public abstract class GenericDaoImpl<E extends SuperEntity> 
        implements GenericDao<E> {

       /** Available for generic methods, so it is not a parameter 
        * for the generic methods. */
       private final Class<E> entityClass;

       protected GenericDaoImpl(Class<E> entityClass) {
         this.entityClass = entityClass;
       }

       // generic implementation ; can be made efficient, as it may 
       // send the orders as a batch
       public void save(E... entities) {
         for(SuperEntity entity : entities) {
           getSession().save(entityClass, entity.getIdent());
         }
         // possibly add flushing, clearing them from the Session ...
       }

       // other generic methods
    }

    public class PersonDaoImpl extends GenericDaoImpl<Person> 
        implements PersonDao {

      /** Constructor, instanciating the superclass with the class parameter. */
      public PersonDaoImpl() {
        super(Person.class);
      }

      /** Specific method. */
      public List<Person> findByAge(int minAge, int maxAge) {
        //....
      }
    }

Wiring 布线

Wiring all the beans is not a fatality. 接线所有的豆子不是致命的。 Nowadays, there are many autowiring policies, you don't have to worry about it. 如今,有许多自动装配策略,您不必担心。 See them in Spring 春季见
http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-annotation-config http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-annotation-config

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM