2017-11-16 3 views
0

これは重複している可能性がありますが、私はこの問題を数日間は苦労しています。何十ものチュートリアル、本、スタックの回答などの後に私は行き詰まっています。一部のリクエストでLazyInitializationExceptionが発生する

これは、セッションでlazyinitializationexceptionや問題点を解消するためにどのようにアプリ

  1. を休止RESTfull春ですか?私はモデルクラスで熱心なフェッチを設定しようとしましたが、それは私のためには機能しませんでした。私はトランザクションアノテーションを持っていますが、それはまだ同じです。私はそれが何とかSpring XML設定で設定されなければならないと確信していますが、それを行う手掛かりはありません。
  2. なぜ.../getAllCountriesが完璧に動作し、/ getCountry/id throwセッションエラー?私はそれらの間に違いは見ません。
  3. コードベースの回答をください。

コントローラー:

@RestController 
    public class CountryController { 

     @Autowired 
     CountryService countryService; 

     @RequestMapping(value = "/getAllCountries", method = RequestMethod.GET, headers = "Accept=application/json") 
     public List<Country> getCountries() { 
      List<Country> listOfCountries = countryService.getAllCountries(); 
      return listOfCountries; 
     } 

     @RequestMapping(value = "/getCountry/{id}", method = RequestMethod.GET, headers = "Accept=application/json") 
     public Country getCountryById(@PathVariable int id) { 
      return countryService.getCountry(id); 
     } 

     // .....  
    } 

DAO:

@Repository 
public class CountryDAO { 

    @Autowired 
    private SessionFactory sessionFactory; 

    public void setSessionFactory(SessionFactory sf) { 
     this.sessionFactory = sf; 
    } 

    public List<Country> getAllCountries() { 
     Session session = this.sessionFactory.getCurrentSession(); 
     List<Country> countryList = session.createQuery("from Country").list(); 
    return countryList; 
    } 

    public Country getCountry(int id) { 
     Session session = this.sessionFactory.getCurrentSession(); 
     Country country = (Country) session.load(Country.class, new Integer(id)); 
     return country; 
    } 
// ...... 

}

サービス:

@Service("countryService") 
public class CountryService { 

    @Autowired 
    CountryDAO countryDao; 

    @Transactional 
    public List<Country> getAllCountries() { 
     return countryDao.getAllCountries(); 
    } 

    @Transactional 
    public Country getCountry(int id) { 
     return countryDao.getCountry(id); 
    } 
} 

エンティティ:

@Entity 
@Table(name="COUNTRY") 
public class Country{ 

    @Id 
    @Column(name="id") 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    int id; 

    @Column(name="countryName") 
    String countryName; 

    @Column(name="population") 
    long population; 

    public Country() { 
      super(); 
    } 
    public Country(int i, String countryName,long population) { 
     super(); 
     this.id = i; 
     this.countryName = countryName; 
     this.population=population; 
    } 

    // getters and setters... 
} 

春の設定のxml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     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.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 

    <annotation-driven /> 

    <resources mapping="/resources/**" location="/resources/" /> 

    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
      destroy-method="close"> 
      <beans:property name="driverClassName" value="org.postgresql.Driver" /> 
      <beans:property name="url" 
        value="..." /> 
      <beans:property name="username" value="postgres" /> 
      <beans:property name="password" value="..." /> 
    </beans:bean> 

    <!-- Hibernate 4 SessionFactory Bean definition --> 
    <beans:bean id="hibernate4AnnotatedSessionFactory" 
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
      <beans:property name="dataSource" ref="dataSource" /> 
      <beans:property name="annotatedClasses"> 
        <beans:list> 
          <beans:value>org.arpit.java2blog.model.Country</beans:value> 
        </beans:list> 
      </beans:property> 
      <beans:property name="hibernateProperties"> 
        <beans:props> 
          <beans:prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect 
          </beans:prop> 
          <beans:prop key="hibernate.show_sql">true</beans:prop> 
        </beans:props> 
      </beans:property> 
    </beans:bean> 

    <context:component-scan base-package="org.arpit.java2blog" /> 

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

    <beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
      <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" /> 
    </beans:bean> 
</beans:beans> 

web.xmlのあなたの問題は、オブジェクトが、あるオブジェクトのプロキシを返さないSession.load()方法から来て

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    version="3.0"> 
    <display-name>Archetype Created Web Application</display-name> 
    <servlet> 
<servlet-name>spring</servlet-name> 
<servlet-class> 
    org.springframework.web.servlet.DispatcherServlet 
</servlet-class> 
<load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
<servlet-name>spring</servlet-name> 
<url-pattern>/</url-pattern> 
</servlet-mapping> 

</web-app> 

答えて

1

非識別子プロパティの最初のアクセス時に初期化されます。

Hibernateのドキュメント:

T負荷(クラスするtheClass、 シリアライズID)

戻り インスタンスが存在すると仮定すると、所与の識別子を有する特定のエンティティ・クラスの永続インスタンス。このメソッドは、非識別子メソッドがアクセスされたときにオンデマンドで初期化された であるプロキシされたインスタンスを返します。

これは、セッションが終了したときにSpringがオブジェクトを整列しようとするときに初期化されることを意味します。あなたは(メンバーの怠惰な戦略に応じて)完全にロードされたオブジェクトを取得するには、DAOにSession.get()代わりのSession.load()を使用して、クラスレベルで@org.hibernate.annotations.Proxy(lazy = false)を追加するか、それを修正するには

セッションがまだオブジェクトの任意のメンバーを呼び出して(読み込みを強制的に行うためにコレクションのList.size()を呼び出すなど)呼び出された場合は、手動で遅延読み込みをトリガーします。

+0

あなたは私の一日を作った。ありがとう。 –

関連する問題