-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: fetch="join" + navigating graph with iterator().ne
PostPosted: Fri Jun 22, 2007 2:40 pm 
Newbie

Joined: Fri Jun 22, 2007 2:03 pm
Posts: 4
Hibernate version:3.2.3

Great software, great documentation, great book (JPA w Hibernate), but it seems i am not smart enough to understand:

I have a question about eager fetching. I am looking at it the whole day and just need some clarification on the reference documentation (and JPA with Hibernate, too).

my mapping with fetch="join"

Code:
   <class name="Member" table="member">
      <id ... />
      <property name="name" not-null="true" />
      <set name="articels" fetch="join" table="articles">
         <key column="member_id" not-null="true" />
         <one-to-many class="Article" />
      </set>
   </class>


And now i read 13.2.3, p. 578 Eager fetching with joins.
and of course the reference documentation which says:

Quote:
The fetch strategy defined in the mapping document affects:
* retrieval via get() or load()
* retrieval that happens implicitly when an association is navigated
* Criteria queries
* HQL queries if subselect fetching is used


Now i try this:

Code:
  public void testFetchJoin () {
      Member member = memberRepository.findMemberById(9L);
      member.getName();
}

and it works. It load() a member and all his articles are loaded
too by an outer join. Fine! Next try:

Code:
  public void testFetchJoin2 () {
   Forum forum = forumRepository.findbyId(1L);
   forum.getMembers().iterator().next().getName();
  }


Hibernate does not load all articles of all members.

I thought, by setting fetch="join" i can assume that the whole non-lazy
object graph is held in memory.

It is the intended behaviour not to load all articles of all members if the whole set is initalized??

If yes, does it mean that calling iterator() on a set is not something to be called "retrieval that happens implicitly when an association is navigated"?

I would really appreciate your help.

kind regards,
janning

PS: As it is a question about clarification of documentation i didn't put all my code into here as advised by the "New Topic form". If this was wrong, please blame me! But i thought about it and came to conclusion that my problem is more understandable without it.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 23, 2007 6:36 pm 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
Kindly explain :
Quote:
Hibernate does not load all articles of all members.


Are you getting any exception?

Regards,
Jitendra


Top
 Profile  
 
 Post subject: full example
PostPosted: Sun Jun 24, 2007 5:45 am 
Newbie

Joined: Fri Jun 22, 2007 2:03 pm
Posts: 4
I'll show you parts of my code, if you are interested in the full story, i uploaded this small test case to
http://www.vygen.de/hibernate-fetch/
as a maven2 project

In my test "testFetchJoinByIteratorNavigation" (see below) i get an LazyInitializationException

Code:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: Member.articles, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97)
at org.hibernate.collection.PersistentSet.size(PersistentSet.java:139)
[...]


this is the failing test:

Code:

   public void testFetchJoinByIteratorNavigation ( )
   {
      Forum forum = (Forum) repository.findById(Forum.class, forumId);
      Member member = forum.getMembers().iterator().next();
      assertEquals(member.getName(), name);
      endTransaction();
      assertEquals(1, member.getArticles().size());
   }


this is my mapping:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping auto-import="true">
   <class name="Forum" table="forum">
      <id name="id">
         <generator class="identity" />
      </id>
      <property name="name" />
      <set name="members" table="members" inverse="true">
         <key column="forum_id" not-null="true" />
         <one-to-many class="Member" />
      </set>
   </class>
   <class name="Article" table="article">
      <id name="id">
         <generator class="identity" />
      </id>
      <property name="title" />

      <many-to-one name="member" column="member_id" class="Member"
         not-null="true">
      </many-to-one>
   </class>
   <class name="Member" table="member">
      <id name="id">
         <generator class="identity" />
      </id>
      <property name="name" />

      <many-to-one name="forum" column="forum_id" class="Forum"
         not-null="true">
      </many-to-one>

      <set name="articles" fetch="join" table="articles"
         inverse="true">
         <key column="member_id" not-null="true" />
         <one-to-many class="Article" />
      </set>
   </class>
</hibernate-mapping>


The classes Forum, Member, Article are simple POJOs, i think they are not relevant to my problem.

this is SQL generated by hibernate for the failing test:
Code:
Hibernate:
    select
        forum0_.id as id0_0_,
        forum0_.name as name0_0_
    from
        forum forum0_
    where
        forum0_.id=?
Hibernate:
    select
        members0_.forum_id as forum3_1_,
        members0_.id as id1_,
        members0_.id as id2_0_,
        members0_.name as name2_0_,
        members0_.forum_id as forum3_2_0_
    from
        member members0_
    where
        members0_.forum_id=?



The fetch="join"-Strategy works if i test it like this:
Code:
   public void testFetchJoinByGraphNavigation ( )
   {
      Member member = (Member) repository.findById(Member.class, memberId);
      assertEquals(member.getName(), name);
      endTransaction();
      assertEquals(1, member.getArticles().size());
   }


this test succeeds.

in my understanding this code "forum.getMembers().iterator().next()" is navigating the object graph and therefore all articles should be loaded.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.