2011-04-08 13 views
10

Estoy usando Hibernate con la primavera.Hibernate ManyToOne con FetchType.LAZY no va a buscar perezoso

Tengo una clase de modelo como esta.


@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; 
    } 
} 

funciona en general, pero la categoría no se carga perezosa, sino con ánimo pronto después de la ForumEntry se ha cargado.

 
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> 

Puede alguien por favor me ayude a entender lo que está sucediendo?

Respuesta

15

Supongo que es causado por el hecho de que sus clases están declaradas como final, por lo tanto, Hibernate no puede generar proxies perezosos para ellas. Intenta eliminar final de las declaraciones de clase.

+0

Gracias Señor, por compartir su sabiduría. – KarlsFriend

1

Parece que la transacción/sesión se cierra después de devolver ForumTopic. Por lo tanto, se convierte en entidad separada. Cuando intenta hacer un getForumCategory no hay sesión para hacer la operación. Puede precargar el ForumCategory en la misma sesión como,

En su getForumTopic, antes de retornar una lista (suponiendo que tiene una getAllForumTopic)

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

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

    return topics; 
} 

Esto obtendrá la ForumCategory en la misma sesión. De lo contrario, debe crear una transacción en la función de llamada de getAllForumTopic y usar la misma transacción donde sea que necesite llamar a getForumCategory.

Cuestiones relacionadas