2011-04-08 4 views
10

봄에 최대 절전 모드를 사용하고 있습니다.FetchType.LAZY가있는 최대 절전 모드 ManyToOne은 게으르지 않습니다.

나는 이와 비슷한 모델 클래스를 가지고있다.


@Entity 
@Table(name = "forumtopic") 
public final class Forumtopic extends AbstractUserTracking implements 
    java.io.Serializable { 

/**SNIP **/ 

    private Forumcategory forumcategory; 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "FkForumcategoryId", nullable = false) 
    public Forumcategory getForumcategory() { 
     return this.forumcategory; 
    } 

    public void setForumcategory(final Forumcategory forumcategory) { 
     this.forumcategory = forumcategory; 
    } 
} 

일반적으로 작동하지만 카테고리는 게으르지 만 ForumEntry가로드 된 후 열심히로드됩니다.

 
Hibernate: 
    select 
     forumtopic0_.PkId as PkId19_0_, 
     forumtopic0_.CreateDate as CreateDate19_0_, 
     forumtopic0_.FkCreateUserId as FkCreate3_19_0_, 
     forumtopic0_.FkLastUserId as FkLastUs4_19_0_, 
     forumtopic0_.LastChange as LastChange19_0_, 
     forumtopic0_.FkForumcategoryId as FkForum10_19_0_, 
     forumtopic0_.PublishCategory as PublishC6_19_0_, 
     forumtopic0_.State as State19_0_, 
     forumtopic0_.Text as Text19_0_, 
     forumtopic0_.Topic as Topic19_0_, 
     forumtopic0_.FkTpUserId as FkTpUserId19_0_ 
    from 
     forumtopic forumtopic0_ 
    where 
     forumtopic0_.PkId=? 
Hibernate: 
    select 
     forumcateg0_.PkId as PkId17_0_, 
     forumcateg0_.CreateDate as CreateDate17_0_, 
     forumcateg0_.Name as Name17_0_, 
     forumcateg0_.FkRequestId as FkReques4_17_0_, 
     forumcateg0_.FkTpUserId as FkTpUserId17_0_ 
    from 
     forumcategory forumcateg0_ 
    where 
     forumcateg0_.PkId=? 

Altough the getter was not called the ForumCategory is loaded right after ForumTopic.

This problems appears in all my @ManyToOne-associations. However @OneToMany associating are loaded lazily.

I am using maven2 for the build. These are my dependencies.

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring</artifactId> 
    <version>2.5.6</version> 
    </dependency> 


    <dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-core</artifactId> 
    <version>3.3.1.GA</version> 
    </dependency> 
    <dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>ejb3-persistence</artifactId> 
    <version>1.0.2.GA</version> 
    </dependency> 
    <dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-annotations</artifactId> 
    <type>jar</type> 
    <version>3.4.0.GA</version> 
    </dependency> 
    <dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-entitymanager</artifactId> 
    <type>jar</type> 
    <version>3.4.0.GA</version> 
    </dependency> 
    <dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-search</artifactId> 
    <version>3.1.0.GA</version> 
</dependency> 

은 누군가가 나에게 무슨 일이 일어나고 있는지 이해하는 데 도움시겠습니까?

답변

15

나는 당신의 클래스가 final으로 선언 되었기 때문에 Hibernate가 그것들을 위해 게으른 프록시를 생성 할 수 없다는 사실에 기인한다고 생각한다. 클래스 선언에서 final을 제거해보십시오.

+0

귀하의 지혜를 공유해 주셔서 감사합니다. – KarlsFriend

1

ForumTopic을 반환 한 후 트랜잭션/세션이 닫힌 것처럼 보입니다. 따라서 분리 된 개체가됩니다. getForumCategory를 수행하려고하면 조작을 수행 할 세션이 없습니다. 당신은 목록을 반환하기 전에이 같은 세션에서 ForumCategory를 가져옵니다

public List<ForumTopic> getAllForumTopic() { 
    <snip> 
    List<ForumTopic> topics = <SNIP: get the list of ForumTopic> 

    for(ForumTopic topic: topics) 
     topic.getForumCategory() 

    return topics; 
} 

(당신은 getAllForumTopic이 가정), 당신의 getForumTopic에서 동일한 세션 등에서

을 ForumCategory 프리 페치 할 수 있습니다. 그렇지 않으면 getAllForumTopic의 호출 함수에서 트랜잭션을 생성하고 getForumCategory를 호출해야하는 곳에서 동일한 트랜잭션을 사용해야합니다.