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: Forcing FetchMode=JOIN?
PostPosted: Fri Apr 23, 2010 9:10 am 
Newbie

Joined: Fri Apr 23, 2010 8:59 am
Posts: 1
Hello,

I have the following example - classes A, B with a 1:n relationship.

Code:
@Entity @Table(name = "A")
@SequenceGenerator(name = "keyid_generator", sequenceName = "A_sequence")
public class A extends AbstractIntKeyIntOptimisticLockingDto {
   private String name;
   private Set<B> bs;

   @OneToMany(mappedBy = "a", fetch = FetchType.EAGER)
   @Fetch(FetchMode.JOIN)
   public Set<B> getBs() {
      return bs;
   }
   public void setBs(Set<B> bs) {
      this.bs = bs;
   }

   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
}

@Entity @Table(name = "B")
@SequenceGenerator(name = "keyid_generator", sequenceName = "B_sequence")
public class B extends AbstractIntKeyIntOptimisticLockingDto {

   private String name;
   private A a;

   @ManyToOne(fetch = FetchType.EAGER)
   @JoinColumn(name = "a", nullable = false,
   unique = false, updatable = false)
   @Fetch(FetchMode.JOIN)
   public A getA() {
      return a;
   }
   public void setA(A a) {
      this.a = a;
   }

   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
}


If I try and select all Bs (getAll() on the DAO), Hibernate issues a select for each A (N+1 SELECT problem) despite my request for JOIN everywhere.

I know how to solve this with HQL, and I can do this which works correctly
Code:
findByCriteria(DetachedCriteria.forClass(B.class)
  .setFetchMode("a", FetchMode.JOIN)
  .setFetchMode("a.bs", FetchMode.JOIN)
);


but can I get it to work directly? Why do I need to explicitly mark "a.bs" as JOINed again?

I'm using hibernate core 3.3.2.GA.


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.