2012-01-03 5 views
0

SessionFactoryをautowiringしてHibernateTemplateを作成するのではなく、HibernateTemplateをautowireするのはいいですか?Spring HibernateTemplate - Autowiring

public class DaoImpl implements Dao { 

private HibernateTemplate hibernateTemplate; 

@Autowired 
public void setSessionFactory(SessionFactory sessionFactory) { 
    hibernateTemplate = new HibernateTemplate(sessionFactory); 
} 
... 
} 

上記コードの代わりに、以下のように指定してもよろしいですか?

public class DaoImpl implements Dao { 

@Autowired private HibernateTemplate hibernateTemplate; 

... 
} 

XMLでHibernateTemplateを設定します。

このアプローチの長所と短所は何ですか?

答えて

0

もちろん、問題ありません。それは最初のアプローチよりも良くないか悪いことではありません。

コードの可読性が向上していると感じる方を選んでください。これが唯一の違いです。主観的です。

0

はい、可能ですが、驚くことではありませんが、DAOがHibernateDaoSupportから拡張されている場合、Springはこれに簡単なメカニズムを提供しています。 getHibernateTemplate()を使用してテンプレートにアクセスできます。私が考えることができる唯一の欠点は、DAOがSpring固有のクラスと結合されていることですが、十分にテストされたライブラリとの結合はOKです。とにかくHibernateTemplateへの直接参照が既にあります。

2

あなたはこれを行うことができますが、あなたは休止状態3.0.1を使用している場合、あなたはHibernateのテンプレートAPIのhere

を引用したJBossのサポートにつきとしても here

NOTE: As of Hibernate 3.0.1, transactional Hibernate access code can also be coded in 
plain Hibernate style. Hence, for newly started projects, consider adopting the standard 
Hibernate3 style of coding data access objects instead, based on 
SessionFactory.getCurrentSession(). 

からされて、次の休止テンプレート【選択を避けることができ

If you plan to use the Spring Hibernate template, then don't. 
The Spring template were useful with Hibernate 2.x because of some mistakes we made 
the main one beeing checked exceptions. This is no longer the case for Hibernate 3.x. 
If you remove this exception wrapping necessity, Spring template are lust an overhead 
on top of the Hibernate API (hiding you the richness of the Hibernate API is some 
cases). 

Hibernate 3の主な変更点の1つは、チェックされていない例外からチェックされていない例外に移動されたことです。記事hereを読んで、これともう1つの詳細についてはhereをご覧ください。

関連する問題