-->
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.  [ 1 post ] 
Author Message
 Post subject: Lazy association chain
PostPosted: Tue Sep 04, 2012 11:02 am 
Newbie

Joined: Tue Sep 04, 2012 10:36 am
Posts: 3
Hi all!
I encountered a problem with lazy association. Here is my simplified class hierarchy:

Code:
@Entity
@Table(name="A")
public class A
{
    @Id
    @GeneratedValue
    @Column(name="ID")
    private int id;
    @OneToMany(mappedBy="a", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
    private Set<B> listB = new HashSet<B>();
}

@Entity
@Table(name="B")
public class B
{
    @Id
    @GeneratedValue
    @Column(name="ID")
    private int id;
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="A_ID")
    private A a;
    @ManyToOne(fetch=FetchType.LAZY)      // ERROR!
    // @ManyToOne(fetch=FetchType.EAGER)  // OK
    @JoinColumn(name="C_ID")
    private C c;
}

@Entity
@Table(name="C")
public class C {
    @Id
    @GeneratedValue
    @Column(name="ID")
    private int id;
}

When I try to read simple structure from db: A->B->C i get the following results:
Code:
System.out.println(a.getId());                  // 1
for (B b : a.getListB()) {                     
    System.out.println(b.getId());              // 1
    C c = b.getC();
    System.out.println(c.getId());              // 0 !!!
}

As you can see instance of C is not properly initialized. After changing fetch type from LAZY to EAGER for field c in class B everything works!

I suspect there is is some CGLIB magic, but can't find a clue nether in the specification nor in Google. Could someone explain this?

Thanks for any help!!!


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

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.