-->
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.  [ 7 posts ] 
Author Message
 Post subject: Bidi OneToMany not lazy?
PostPosted: Thu Nov 02, 2006 11:11 am 
Beginner
Beginner

Joined: Tue Nov 30, 2004 6:19 am
Posts: 29
Location: Germany
Hi.

I try to get a bidi OneToMany loaded lazy ... but the connection is fetched eager all the times, what's the reason?

Used Version is Hbn3.2GA, Annotation3.2GA

Example is:
Code:
@Entity
public class Customer {
  ..
  @OneToMany (mappedBy="customer", cascade = CascadeType.ALL,   fetch=FetchType.LAZY)
  public Set<Film> getFilms() {return films;}
}

@Entity
public class Film {
  ..
  @ManyToOne (fetch=FetchType.LAZY) @JoinColumn (name="FK_CUSTOMER")
  public Customer getCustomer() {
    return customer;
  }
}


Kind regards,
Michael


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 6:38 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
it should work

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Example Code
PostPosted: Mon Nov 13, 2006 4:00 am 
Beginner
Beginner

Joined: Tue Nov 30, 2004 6:19 am
Posts: 29
Location: Germany
I can provide some example code:
CommitTransaction closes the session also and beginTransaction get's a new one from SessionFactory.getCurrentSession() ... as recommended.

If you look at the produced output, the "from Film" direktion works as expected, but the "from Customer" direction doesn't - Films are loaded eagerly. And no, the toString() method of customer doesn't touch the films set ...

So I've no Idea, what's wrong.

Kind regards,
Michael

Btw -I'm very impressed by the output of the second "System.out.println("Customer ClassName: " + film.getCustomer().getClass().getName() );" in queryDataFromFilm method - good idea and tough work :-)

Code:
private void insertAndWireData() {
       System.out.println("+++++++++++++++ insertAndWire +++++++++++++++++++");
       
       // insert
       beginTransaction();
       
        Film film1 = new Film(1111, "Der mit dem Wolf tanzt");
        Film film2 = new Film(2222, "Herr der Ringe");
        Film film3 = new Film(3333, "King Kong");
        getSession().save(new Film(4444, "Jagd auf Roter Oktober"));
        customer1 = new Customer("Fritz Frei");
       
        customer1.addFilm(film1);
        customer1.addFilm(film2);
        customer1.addFilm(film3);
       
        getSession().save(customer1);
        getSession().save(new Customer("Bernd Bauer"));
       
        commit();
    }

    private void queryDataFromFilm() {
       System.out.println("+++++++++++++++ queryDataFromFilm +++++++++++++++++++");
       
       beginTransaction();
        List<Film> films = getSession().createQuery("from Film").list();
        for (Film film : films) {
           System.out.println(film);
           if(film.getCustomer() != null) {
              System.out.println("Customer ClassName: " + film.getCustomer().getClass().getName() );
              System.out.println("Customer.id: " + film.getCustomer().getId());
              System.out.println("Customer: " + film.getCustomer());
              System.out.println("Customer ClassName: " + film.getCustomer().getClass().getName() );
           }
            System.out.println();
        }
        commit();
    }
   
    @SuppressWarnings("unchecked")
    private void queryDataFromCustomer() {
       System.out.println("+++++++++++++++ queryDataFromCustomer +++++++++++++++++++");
       
       beginTransaction();
        Customer customer = (Customer) getSession().get(Customer.class, customer1.getId());
       
            System.out.println(customer);            
            System.out.println("Films ClassName: " + customer.getFilms().getClass().getName());
            System.out.println("Films.size: " + customer.getFilms().size());
            System.out.println("Films: " + customer.getFilms());
            System.out.println();
       
        commit();
    }


Code:
Produced output:
+++++++++++++++ queryDataFromFilm +++++++++++++++++++
Hibernate:
    select
        film0_.id as id0_,
        film0_.FK_CUSTOMER as FK4_0_,
        film0_.inventarNr as inventarNr0_,
        film0_.title as title0_
    from
        Film film0_
Film: 4444, Jagd auf Roter Oktober

Film: 3333, King Kong
Customer ClassName: b_MappingErweitert.e_Lazy.model.Customer$$EnhancerByCGLIB$$4693929a
Customer.id: 3
Hibernate:
    select
        customer0_.id as id1_0_,
        customer0_.name as name1_0_
    from
        Customer customer0_
    where
        customer0_.id=?
Customer: Customer: 3, Fritz Frei
Customer ClassName: b_MappingErweitert.e_Lazy.model.Customer
...

+++++++++++++++ queryDataFromCustomer +++++++++++++++++++
Hibernate:
    select
        customer0_.id as id1_0_,
        customer0_.name as name1_0_
    from
        Customer customer0_
    where
        customer0_.id=?
Hibernate:
    select
        films0_.FK_CUSTOMER as FK4_1_,
        films0_.id as id1_,
        films0_.id as id0_0_,
        films0_.FK_CUSTOMER as FK4_0_0_,
        films0_.inventarNr as inventarNr0_0_,
        films0_.title as title0_0_
    from
        Film films0_
    where
        films0_.FK_CUSTOMER=?
Customer: 3, Fritz Frei
Films ClassName: org.hibernate.collection.PersistentSet
Films.size: 3
Films: [Film: 1111, Der mit dem Wolf tanzt, Film: 2222, Herr der Ringe, Film: 3333, King Kong]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 13, 2006 6:42 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
getFilms().size() does

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 14, 2006 3:10 am 
Beginner
Beginner

Joined: Tue Nov 30, 2004 6:19 am
Posts: 29
Location: Germany
Hi Emmanuel,

you're right, but I expect the output from

Code:
            System.out.println(customer);           
            System.out.println("Films ClassName: " + customer.getFilms().getClass().getName());


definitely before the "select ... from film ..." !
So the question is, why the second db-access is executed before the content of the PersistentSet is accessed the first time.


Kind regards,
Michael


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 14, 2006 11:28 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Either, customer.toString() does something or the output is unordered.
Try assertTrue( Hibernate.isInitialized( customer.getFilms() ); before any System.out

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 14, 2006 6:29 pm 
Beginner
Beginner

Joined: Tue Nov 30, 2004 6:19 am
Posts: 29
Location: Germany
Hi Emmanuel,

you mad my day :) I found the error in customer.setFilms ... sorry for not posting the complete customer code ...
I will switch to field access and the special setter will be okay, I think :-)

Thx and kind regards,
Michael

Code:
@OneToMany (mappedBy="customer", cascade = CascadeType.ALL, fetch=FetchType.LAZY)
public Set<Film> getFilms() {return films;}
public void setFilms(Set<Film> films) {
       // reset old backrefs
       for (Film film : this.films) {
          film.setCustomer(null);         
      }
       // set new backref
       for (Film film : films) {
         film.setCustomer(this);
      }
       // remember the given films
       this.films = films;
    }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.