ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • org.hibernate.LazyInitializationException
    java 2021. 3. 28. 21:59

    System의 하이버네이트를 통해 code를 함께 호출하는 매핑코드입니다.

     

    <map name="code" table="codes" lazy="true" cascade="all" order-by="position asc" fetch="join">
      <key column="codeId" />
      <map-key type="string" column="codeName" length="64"/>
      <composite-element class="com.project.System.Code">
        <property name="codeValue" 	type="string" 	column="codeValue" 	length="200"/>
        <property name="label" 		type="string" 	column="label" 		length="100"/>
      </composite-element>
    </map>

     

    원인

     

     서버에서 System이 생성된 이후 비동기로 수행할때 처음 생성한 System은 생성된 transaction 내에서 수행했기 때문에 문제가 없지만, 해당 transaction 내에서 mapping되어있는 code를 호출하지 않았다면 매핑된 code는 값이 존재하지 않습니다.

     

     객체는 전달되지만 System 객체의 lazy - true로 되어있는 매핑객체는 이전 세션(transaction)에서 초기화가 되어있지 않았으며, 수행되는 메소드에서는 hibernate transaction을 갖고있지 않기 때문에 no Session 에러를 내보내게 됩니다. 

     

    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role:  com.project.System.Code, could not initialize proxy - no Session

     

     

    해결

     

    <map name="code" table="codes" lazy="false" cascade="all" order-by="position asc" fetch="join">
      <key column="codeId" />
      <map-key type="string" column="codeName" length="64"/>
      <composite-element class="com.project.System.Code">
        <property name="codeValue" 	type="string" 	column="codeValue" 	length="200"/>
        <property name="label" 		type="string" 	column="label" 		length="100"/>
      </composite-element>
    </map>

     

     lazy="false"로 해결합니다.

     code 객체에 대해 액션이 끝난 이후 객체가 없을때 불러와야 되기 때문에 hibernate와 새로운 세션을 맺고 조회가 가능하게 됩니다.

     

     

    hibernate-mapping LAZY

    ="true" 

     객체를 호출하면서 매핑객체를 검색하지만, 객체를 로드 할 때가 아니라 매핑객체가 처음 액세스 될 때 조회되는 것을 의미합니다.

     ( Lazy-load, 지연로딩 )

     

    = "false"

     객체를 호출할때 lazy가 활성화되지 않은 매핑객체도 함께 조회합니다.

     ( Eager-load, 즉시로딩 )

     

    댓글

Designed by Tistory.