2017-02-28 2 views
0

私はsessionFactoryを私のDAOImplファイルでautowiringして使用していました。 特定のDAOメソッドで「」というメッセージが表示されるようになるまで、状況はうまくいきました。考えられる理由と解決策を探した後、applicationContext.xml私が使用している方法が間違って設定されている可能性があることを認識しましたです。Spring 4で永続性EntityManagerをSessionFactoryと共に使用する方法hibernate 5?

以下は私のapplicationContext.xmlをファイルの内容です:

<?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:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> 

    <!-- Enables the Spring MVC @Controller programming model --> 

    <!-- enables cors (cross origin request) for all urls --> 
    <mvc:cors> 
     <mvc:mapping path="/**" /> 
    </mvc:cors> 

    <!-- <mvc:annotation-driven /> --> 

    <mvc:annotation-driven> 
     <mvc:message-converters> 
      <!-- Use the HibernateAware mapper instead of the default --> 
      <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 
       <property name="objectMapper"> 
        <bean class="com.typjaipur.core.objectmapper.HibernateAwareObjectMapper" /> 
       </property> 
      </bean> 
     </mvc:message-converters> 
    </mvc:annotation-driven> 

    <context:component-scan base-package="com.typjaipur" /> 

    <bean id="messageSource" 
     class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
     <property name="basename" value="classpath:messages" /> 
     <property name="defaultEncoding" value="UTF-8" /> 
    </bean> 

    <bean id="propertyPlaceholder" class="com.typjaipur.core.config.EnvironmentPropertyPlaceholderConfigurer"> 
     <property name="locations"> 
      <list> 
       <value>classpath:jdbc.properties</value> 
       <value>classpath:core.properties</value> 
       <value>classpath:mailer.properties</value> 
      </list> 
     </property> 
    </bean> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="${driver}" /> 
     <property name="url" value="${url}" /> 
     <property name="username" value="${username}" /> 
     <property name="password" value="${password}" /> 
    </bean> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="packagesToScan" value="com.typjaipur.model" /> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.max_fetch_depth">3</prop> 
       <prop key="hibernate.default_batch_fetch_size">4</prop> 
       <prop key="hibernate.jdbc.fetch_size">50</prop> 
       <prop key="hibernate.jdbc.batch_size">20</prop> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
       <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
       <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> 
      </props> 
     </property> 
    </bean> 

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

    <tx:annotation-driven transaction-manager="hibernateTransactionManager" /> 

    <mvc:interceptors> 
     <bean class="org.springframework.orm.hibernate5.support.OpenSessionInViewInterceptor"> 
      <property name="sessionFactory"> 
       <ref bean="sessionFactory" /> 
      </property> 
     </bean> 
     <mvc:interceptor> 
      <mvc:mapping path="/**" /> 
      <!-- excluded urls --> 
      <mvc:exclude-mapping path="/" /> 
      <bean class="com.typjaipur.interceptor.ApiAuthenticationInterceptor" /> 
     </mvc:interceptor> 
    </mvc:interceptors> 


    <!-- Enables swgger ui --> 
    <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" /> 
    <mvc:resources mapping="/webjars/**" 
     location="classpath:/META-INF/resources/webjars/" /> 

    <!-- Include a swagger configuration --> 
    <bean name="/applicationSwaggerConfig" class="com.typjaipur.config.SwaggerConfig" /> 

    <bean id="multipartResolver" 
     class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
     <!-- one of the properties available; the maximum file size in bytes --> 
     <!-- <property name="maxUploadSize" value="100000"/> --> 
    </bean> 
</beans> 

以下は、私があまりにも働いているEntityManagerを使用していますかのDAOImplコード例ですが、私は多分その正しくないと感じています。

@Repository 
public class BusinessDetailsDAOImpl extends BaseDAOImpl<BusinessDetails, Long> implements BusinessDetailsDAO { 

    @Autowired 
    private SessionFactory sessionFactory; 

    @Override 
    public List<BusinessDetails> searchBusiness(BusinessSearchDTO businessSearchDTO, List<Long> businessIds) { 

     EntityManager entityManager = sessionFactory.getCurrentSession().getEntityManagerFactory().createEntityManager(); 
     CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); 
     CriteriaQuery<BusinessDetails> query = criteriaBuilder.createQuery(BusinessDetails.class); 
     Root<BusinessDetails> businessDetails = query.from(BusinessDetails.class); 

     List<Predicate> predicates = new ArrayList<Predicate>(); 
...//rest of code 

私はすべての私のプロジェクトの上にそれを使用しているとして、私はこの時点でsessionFactoryを避けることが厳しいです。とにかくEntityManagerSessionFactoryを一緒に使用できるようにxmlファイルを構成できるのですか?

EntityManagerを構成するいくつかの例がありましたが、いずれもSessionFactoryに関連するxmlファイルの行を追加していません。だから私はこれで混乱している。あなたがentityManagerを取得するための呼び出しsessionFactory.getCurrentSession().getEntityManagerFactory().createEntityManager()必要はありませんので、代わりにLocalSessionFactoryBeanを使用してのEntityManagerを作成する

+0

問題は自分で導入されます。 Yuoはエンティティマネージャインスタンスを自分で作成しています。それぞれ、新しい「Connection」を作成しています。あなたはエンティティマネージャを管理しておらず、最終的にはあなたは不足します。別のものは実稼働用ではなく、適切な接続プールを使ってテストするのに適した 'DriverManagerDataSource'を使用しません。 –

+0

'DriverManagerDataSource'には何が推奨されますか?あなたは教えていただけますか? –

+0

適切な接続プールが指定されているので、さまざまなオプションがあります。 –

答えて

0

使用LocalContainerEntityManagerFactoryBean

<!-- Create a datasource --> 
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 

    <property name="driverClassName" value="${driver}" /> 
    <property name="url" value="${url}" /> 
    <property name="username" value="${username}" /> 
    <property name="password" value="${password}" /> 

</bean> 

<!-- Create an Hibernate to Jpa adapter --> 
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 

    <property name="generateDdl" value="true" /> 
    <property name="showSql" value="false" /> 
    <property name="database" value="MYSQL" /> 

</bean> 

<!-- persistenceUnitManager is optional --> 
<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"> 

    <property name="defaultDataSource" ref="dataSource"/> 

</bean> 

<!-- create entityManagerFactory --> 
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 

    <property name="persistenceUnitManager" ref="persistenceUnitManager"/> 
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter" /> 

    <property name="jpaProperties"> 
     <props> 
      <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
     </props> 
    </property> 

</bean> 

<!-- and create transactionManager --> 
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
    <property name="entityManagerFactory" ref="entityManagerFactory"/> 
</bean> 
:春の設定ファイルでは

  1. はと、この電流設定を置き換えます
    1. このように使用してください:
    @Repository 
    public class BusinessDetailsDAOImpl extends BaseDAOImpl<BusinessDetails, Long> implements BusinessDetailsDAO { 
    
        @PersistenceContext 
        protected EntityManager entityManager; 
    
        @Override 
        public List<BusinessDetails> searchBusiness(BusinessSearchDTO businessSearchDTO, List<Long> businessIds) { 
    
         // use directly entityManager instead of sessionFactory.getCurrentSession().getEntityManagerFactory().createEntityManager(); 
    
         CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); 
    
         // ... 
    
    } 
    

    編集:次のようにあなたの設定を更新します。

    <property name="jpaProperties"> 
        <props> 
         <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> 
         <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
    
         <!-- add this line --> 
         <prop key="hibernate.enable_lazy_load_no_trans">true</prop> 
        </props> 
    </property> 
    
+0

この設定で 'OpenSessionInViewInterceptor'を使用するにはどうしたらいいですか? –

+0

Hibernate 4を使用しているので、 'OpenSessionInViewInterceptor'はもう必要ありません。次のプロパティ' hibernate.enable_lazy_load_no_trans'を 'true'に設定するだけで、Hibernateは必要なときにセッションを作成します。 –

+0

Hibernate 4?私はHibernate 5を使用しています。not 4。 –

関連する問題