-->
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" makes additional queries in one-to-many - bug?
PostPosted: Thu Jul 16, 2009 9:19 am 
Newbie

Joined: Thu Jul 16, 2009 9:04 am
Posts: 2
I find that in a one-to-many scenario, fetch="join" behaves the same as fetch="select". I understand that fetch="join" needs to make a single query to the database using joins. Whereas, I found that Hibernate is always making additional queries on the child table(the number of queries can of course be controlled by batch-size option).

Is this a known bug?.
If so, why is this not clearly specified in the documentation itself? It seems hundreds of hibernate users are asking this question.
Why is this not being put in the faq?
Can somebody say clearly it is a known bug or it is not?

I am using Hibernate 3.3.1.GA

Please pardon me if I had overlooked something.


Top
 Profile  
 
 Post subject: Re: fetch="join" makes additional queries in one-to-many - bug?
PostPosted: Thu Jul 16, 2009 5:12 pm 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
You can try this code:
Code:
public class Author implements Serializable {
        @OneToMany(mappedBy = "author", fetch = FetchType.LAZY)
   @org.hibernate.annotations.Fetch(org.hibernate.annotations.FetchMode.JOIN)
        //you can change JOIN to SUBSELECT
   public List<Book> getBooks() {
      return books;
   }
.....
}


Code:
    Session session = HibernateUtil.getSession();
    session.beginTransaction();

    Criteria c = session.createCriteria(Author.class);
    Disjunction disjunction = Restrictions.disjunction();
    c.add(disjunction);
    List<Author> list = c.list();
    for(Author author : list){
        System.out.println("No. of books : " + author.getBooks().size());
    }
    session.getTransaction().commit();


is this what you mean?


Top
 Profile  
 
 Post subject: Re: fetch="join" makes additional queries in one-to-many - bug?
PostPosted: Wed Jul 29, 2009 9:14 am 
Newbie

Joined: Thu Jul 16, 2009 9:04 am
Posts: 2
Hi,

Thank you very much for your reply. There was both success and failure in the suggestion given by you.

I have parent table A and child table B.
Table A has 8 records. Table B has 9 records

Success: Using Criteria generated a single query joining both the tables.

Failure: The returned list using "fetch=subselect" was different from the resulting list using "fetch=join" (with no change in code at all).
Here is the difference: The list returned using "fetch=subselect" returns exactly the same number of items as the number of rows in Table A (i.e., one list item per row in Table A). This is the expected behavior. When I used "fetch="join", the list had more items (i.e., List had one record for each row of the join operation. Example: When one row of table A has 4 child records in Table B, the query returns 4 separate rows and hence hibernate populates the list with 4 items.) Hibernate must be returning only one item in the List per one row of Table A. Do you agree?

-------Code fragment begins------

s = sessionFactory.openSession();
Criteria c = s.createCriteria(A.class);
Disjunction d = Restrictions.disjunction();
c.add(d);
l = c.list();

-------Code fragment ends--------


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.