-->
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: EAGER fetch does not work in one select
PostPosted: Tue Mar 04, 2008 12:41 pm 
Newbie

Joined: Tue May 22, 2007 10:52 am
Posts: 13
Hi,

I am setting Eager fetching for OneToOne and ManyToOne relationships, and even though I specify @Fetch(FetchMode.JOIN), it does not work and is still performing additional selects.

To be more concrete the "main" entity is RfqRequest and it links to CoreCurrency (many-to-one) and RfqRequestOciParam(one-to-one).

Please help!

The HQL is very simple:

Code:
entityManager.createQuery("from RfqRequest req").getResultList()


And it generates many sqls like this:
Code:
18:20:52,715 INFO  [STDOUT] Hibernate: /* from RfqRequest req  */ select ...{removed the selected columns}...  from dbo.RFQ_Request rfqrequest0_
18:20:52,871 INFO  [STDOUT] Hibernate: /* load com.ibx.ibxrequest.model.CoreCurrency */ select  ...{removed the selected columns}... from dbo.CORE_Currency corecurren0_ where corecurren0_.CurrencyId=?
18:20:52,871 INFO  [STDOUT] Hibernate: /* load com.ibx.ibxrequest.model.RfqRequestOciParam */ select  ...{removed the selected columns}... from dbo.RFQ_RequestOCIParam rfqrequest0_ where rfqrequest0_.RequestId=?


Here is an excerpt from RfqRequest entity with the 2 links:
Code:
@Entity
@Table(name = "RFQ_Request", uniqueConstraints = {})
public class RfqRequest implements java.io.Serializable {
....
   @ManyToOne(cascade = {}, fetch = FetchType.EAGER)
   @Fetch(FetchMode.JOIN)
   @LazyToOne(LazyToOneOption.FALSE)
   @JoinColumn(name = "CurrencyId", unique = false, nullable = true, insertable = true, updatable = true)   
   public CoreCurrency getRfqCurIso() {
      return this.rfqCurIso;
   }

   @PrimaryKeyJoinColumn
   @OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
   @Fetch(FetchMode.JOIN)
   @LazyToOne(LazyToOneOption.FALSE)
   public RfqRequestOciParam getOciParams() {
      return ociParams;
   }

....
}


Here is an excerpt from the referred CoreCurrency entity:
Code:
@Entity
@Table(name = "CORE_Currency", uniqueConstraints = { @UniqueConstraint(columnNames = { "CurISO" }) })
public class CoreCurrency implements java.io.Serializable {
.....   
        @Id
   @GeneratedValue
   @Column(name = "CurrencyId", unique = true, nullable = false, insertable = true, updatable = true)
   public Integer getCurrencyId() {
      return this.currencyId;
   }
....
}


And the RfqRequestOciParam entity
Code:
@Entity
@Table(name = "RFQ_RequestOCIParam",  uniqueConstraints = {})
public class RfqRequestOciParam implements java.io.Serializable {
...
   @Id
   @GeneratedValue(generator = "myForeignGenerator")
   @GenericGenerator(   name = "myForeignGenerator", strategy = "foreign",
                  parameters = @Parameter(name = "property", value = "rfqRequest") )   
   @Column(name = "RequestId", unique = true, nullable = false, insertable = true, updatable = true)
   public Integer getRequestId() {
      return this.requestId;
   }
   @OneToOne(cascade = {}, fetch = FetchType.LAZY, mappedBy="ociParams" )
   public RfqRequest getRfqRequest() {
      return this.rfqRequest;
   }
...
}


Hibernate version:
[Version] Hibernate Annotations 3.3.0.GA
[Environment] Hibernate 3.2.4.sp1
[Version] Hibernate EntityManager 3.3.1.GA
[Version] Hibernate Validator 3.0.0.GA
[Version] Hibernate Search 3.0.0.GA

Name and version of the database you are using:
[SettingsFactory] RDBMS: Microsoft SQL Server, version: 08.00.2039
[SettingsFactory] JDBC driver: jTDS Type 4 JDBC Driver for MS SQL Server and Sybase, version: 1.2
[Dialect] Using dialect: org.hibernate.dialect.SQLServerDialect


Top
 Profile  
 
 Post subject: Re: EAGER fetch does not work in one select
PostPosted: Tue Mar 04, 2008 12:53 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
HQL does not look at the fetch settings. If there is anything that needed to be fetched you have specify it. for example:

Code:
select p from Person p inner join fetch p.addresses



the fetch join part does the trick for you.



Farzad-


Top
 Profile  
 
 Post subject: Re: EAGER fetch does not work in one select
PostPosted: Wed Mar 05, 2008 3:16 am 
Newbie

Joined: Tue May 22, 2007 10:52 am
Posts: 13
farzad wrote:
HQL does not look at the fetch settings. If there is anything that needed to be fetched you have specify it. for example:

Code:
select p from Person p inner join fetch p.addresses



The hibernate docs say that there is an option: you can either specify the fetch setting in the model (like I did) or individually for each HQL.

In my case I prefer to specify the EAGER setting in the model because I don't want to modify all HQLs in my application - those entities need to be eagerly fetched every time.

(Actually my problem is not that it is not eagerly fetched - it is fetched eagerly, but is not fetched in a JOIN style - instead it is fetched with multiple (n+1) selects)

Please help

Thanks


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.