2016-12-17 9 views
-2

私のアプリケーションでは、我々は次のようなアーキテクチャがあります。春EJB統合

  • Webレイヤー:EJB
  • DAO層:JSF +リッチ
  • サービスレイヤ顔EJBクラスの[JDBCクエリで構成さ]​​

DAOレイヤでJDBCではなく、ORMフレームワークとしてHibernateを使用したいと考えています。 DAOレイヤーでHibernateを統合するために、Spring ORM機能を使用したいと思います。現在、直面している課題:

DAOレイヤークラスはステートレスEJBクラスです。だから、次のように私が迎撃のために行く必要が、EJBクラス内の春DIを使用するには:私はこの例外を取得していますsessionFactory.getCurrentSession()

@Stateless(mappedName = "myAppDao") 
@Interceptors(SpringBeanAutowiringInterceptor.class) 
public class MyAppDaoImpl implements MyAppDaoRemote { 

@Autowired 
private SessionFactory sessionFactory; 

@Override 
public void getSession() { 
    if(sessionFactory!=null){ 
     Session session = null; 
     try{ 
      session = sessionFactory.getCurrentSession(); 
     }catch (Exception e) { 
      session = sessionFactory.openSession(); 
      System.out.println("Exception:"+e.getMessage()); 
     } 
} 

例外:トランザクション同期セッションを取得できませんでした現在のスレッド用。

なぜ私はサービスレイヤーにEJBクラスがあるので、私はサービスレイヤーでSpringトランザクションを使用できません。

また、sessionFactory.openSession()を使用したくない場合は、手動でセッションを終了する必要があります。

これらは私のSpring構成ファイルです:

1)beanRefContext.xml - >インターセプター

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> 

    <bean id="businessBeanFctory" class="org.springframework.context.support.ClassPathXmlApplicationContext"> 
      <constructor-arg value="classpath*:daoConfig.xml" /> 
    </bean> 
</beans> 

2)daoConfig.xmlで使用される - > Hibernate構成が

<?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:p="http://www.springframework.org/schema/p" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd 
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> 

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="packagesToScan" value="com.myapp.model"/> 
     <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> 
      <prop key="hibernate.show_sql">false</prop> 
      </props> 
     </property> 
    </bean> 

    <jee:jndi-lookup id="dataSource" jndi-name="jdbc/MyAppDS" expected-type="javax.sql.DataSource"/> 

    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>  
    <tx:annotation-driven transaction-manager="transactionManager" /> 

    </beans> 

私が知っていますSpringと一緒にEJBを使用することは意味がありません。しかし、プロジェクトレベルでの制約のため、当面はEJBを削除できません。

Springを介してEJBクラス内でHibernateの現在のセッションを取得できる解決策はありますか?

+1

"DAOレイヤでJDBCではなくORMフレームワークとしてHibernateを使用したいと思います。DAOレイヤでHibernateを統合するためにSpring ORM機能を使用したいと思います。おそらく私はその点を理解していないかもしれませんが、EJBクラスで構成されるDAOレイヤーでHibernateを使用することが第一の目標である場合、EJBをHibernateに直接統合しようとしない理由は何ですか? – hammerfest

+0

hibernate.cfg.config.xmlファイルを使ってHibernateのみを使用する場合、Springのbeacauseを追加する必要があります。次に、トランザクションのオープンとクローズ、およびコミット操作を気にする必要があります。コードレベルから呼び出す必要があります(transaction.commit )これらのすべてをSpringコンテナで管理したい。 –

答えて

0

おはJBossASを使用している場合/ WildFlyは、あなただけの必要があります。

@Stateless(mappedName = "myAppDao") 
public class MyAppDaoImpl implements MyAppDaoRemote { 

    @PersistenceContext 
    private Session session; 

    public void someDaoMethod(YourEntity e) { 
     // use session directly 
     // Transactions are automatically managed by the EJB container 
     // because that's one of EJB's raison d'être 
    } 

} 

は、そのあなたの(伝統的な)ことを確認した構成は、JTAトランザクションを指定休止します。

詳細については、WildFly JPA Reference Guideを参照してください。

あなたは、あなたはまだあなたが持っているSpring構成を使用しますが、含めることを忘れないでくださいすることができ、これらのJavaEE実装の1つを使用していない場合:

<property name="jtaTransactionManager" value="transactionManager"/> 

あなたsessionFactory豆に。

さらに、WebLogicまたはWebSphereを使用している場合は、WebLogicJtaTransactionManagerまたはorg.springframework.transaction.jta.WebSphereUowTransactionManagerなどのプラットフォーム依存のJTAトランザクションマネージャを指定する必要があります。

0

私はこのdefinition.After sessionFactory豆の下<property name="jtaTransactionManager" ref="transactionManager"/> を追加した方法

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:jee="http://www.springframework.org/schema/jee" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd 
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="packagesToScan" value="com.oms.model"/> 
    <property name="jtaTransactionManager" ref="transactionManager"/> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> 
      <prop key="hibernate.show_sql">true</prop> 
     </props> 
    </property> 
</bean> 

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/OMSDS" expected-type="javax.sql.DataSource"/> 

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/> 


</beans> 

次の私のdaoConfig.xmlを変更している私は私のDAO層のクラスに

session = sessionFactory.getCurrentSession();

を使用することができています。

関連する問題