2

最近、HibernateとSpringを使い始めました。

最初は、sessionFactoryとopenSessionをbeginTransactionと一緒に使用してdb呼び出しを行うように言われました。

後でdaoについて聞いたので、インターフェイスを作成し、このインターフェイスをクラスに実装してから、このクラスにHibernateDAOSupportを拡張させることで使用を開始しました。

同僚がこの方法は非推奨であり、HibernateDAOSupportクラスを使用すべきではないと私に言うまで、これはかなり堅実であると思いました。ただし、代わりに、dao実装クラスのSessionFactoryインスタンスと、新しい個別のサービスクラスのそのクラスのインスタンスを操作します。これも良い方法のように思えたので、私はこの道をたどることにしました。

ちょうど今、この方法は新しいバージョンの春でも廃止されることを読みました...だから私の質問は、冬眠と春を一緒にする正しい最新の方法は一体何ですか?

エンティティマネージャーについての話も聞いたことがありますが、それはどういうことですか?

私はそれらを使用する一般的な方法を探しています。一般的な規則に例外がある場合は、これらの例外の例も提供してください。

4

2 に答える 2

5

HibernateをSpringに統合する多くの方法の1つです...

サービス(またはDAO)クラスは次のように単純に見えるはずです:-

// annotate this class so that Spring is aware of it 
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private SessionFactory sessionFactory;

    // this is straight-up HQL... really, no magic here
    @Override
    @SuppressWarnings("unchecked")
    public Collection<Employee> getAllEmployees() {
        return sessionFactory.getCurrentSession()
                .createQuery("from Employee e order by e.lastName, e.firstName")
                .list();
    }
}

ここで、を構成する必要があります。構成sessionFactoryしないと、自動配線が失敗します。だから、これはあなたがそれを設定する方法です:-

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:jee="http://www.springframework.org/schema/jee"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">

    <!-- 
        When creating the session factory, point it to your existing hibernate.cfg.xml.
        You can also port your entire Hibernate configuration and HBM mappings here, but
        for simplicity sake, I'll reference the existing hibernate.cfg.xml here so that we 
        are not cluttering Spring configuration file with Hibernate-specific configuration.
    -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    </bean>

    <!-- If you are running in production, you will want to use JNDI connection -->
    <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/myapp"/>

    <!-- If you are running testcases, you might want to use JDBC instead -->
    <!--
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
        <property name="url" value="jdbc:jtds:sqlserver://machine/myapp"/>
        <property name="username" value="myapp"/>
        <property name="password" value="myapp"/>
    </bean>
    -->

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <aop:config proxy-target-class="true">
        <aop:advisor pointcut="execution(* com.myapp..*.*(..))" advice-ref="txAdvice"/>
    </aop:config>

</beans>

トランザクションを構成するには、2つの方法があります。1)@Transactional適切なトランザクション処理を必要とするクラス(または特定のメソッド)を使用して注釈を付けることができます。2)AOPを使用して、すべてのコードをトランザクションでラップします。

com.myapp上記の例では、すべてのコードをトランザクション付きの基本パッケージでラップしています。ポイントカットを変更(または追加)して、トランザクションラッパーを減らすことができます。

于 2013-01-16T23:59:57.130 に答える
4

春のHibernateに関しては、2013年に関しては、HibernateDaoSupportは非推奨になりました(そしてHibernateTemplateも)。Hibernate4.0では機能しません。

現在のデフォルトの選択は、 Hibernate固有の注釈付き プロパティで@Transactional注釈付きクラスを使用してJPA構成の永続コンテキストを自動配線することだと思います。sessionFactory.getCurrentSession()@PersistenceContextEntityManager

JPAは標準ですが、Hibernateは標準ではありませんが、ほとんどのHibernate固有のものはJPA構成で機能します(もちろん、標準に準拠しなくなり、別のJPA実装に切り替えるのが難しくなります)。純粋なHibernateでのみ使用可能です。たとえば、JPA2.0より前のセッションからスタンドアロンオブジェクトをデタッチすることはできませんでした。標準を変更するよりも、独自のフレームワークにいくつかの新機能を実装する方が簡単ですが、ほとんどの場合、JPA2.0が適していると思います。

StackOveflow 123などにも同様の質問がたくさんあります

JPA構成を使用するための良い議論は、Spring Data JPA(リポジトリーの実装を簡素化するフレームワーク)と連携できることです(インターフェースと汎用リポジトリーベースのクラスの間で並列階層を維持する代わりに、インターフェースを宣言するだけで済みます)。

于 2013-01-16T23:24:10.410 に答える