-->
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.  [ 22 posts ]  Go to page Previous  1, 2
Author Message
 Post subject:
PostPosted: Thu Apr 09, 2009 5:26 pm 
Newbie

Joined: Mon Feb 02, 2009 8:56 am
Posts: 6
mars1412 wrote:
  • use HQL: it'll ignore the global eager fetching anyway

Hi,
I'm using annotations in hibernate 3.2.5 to map a many-to-one association between two classes and despite I'm using HQL queries, it doesn't ignore eager fetching

Here's my class:
Code:
@Entity
public class First {
   ...
   private Second second;
   ...
   @ManyToOne()
   @JoinColumn(name = "secondId")

   public Second getSecond() {
      return second;
   }
   ...
}

The query:
Code:
session.createQuery("from First").list();

After running it, I can see Hibernate is issuing a query for "First" entity, and then, another one for "Second".

I could set "fetch=FetchType.LAZY" in the mapping, but 90% of the cases I need eager fetching.

How can I tell HQL not to fetch the association? Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 10, 2009 4:23 am 
Beginner
Beginner

Joined: Tue Nov 06, 2007 5:13 am
Posts: 28
hmm.. I'm only using criteria queries and thus I only know what I have read in the (excellent) book 'Java Persistence with Hibernate':
Quote:
HQL and JPA QL ignore any fetching strategy you’ve defined in mapping metadata.....

Thus, I thought it shouldn't do eager fetching.
But maybe I've misinterpreted this paragraph, or maybe that has been changed in newer versions.


Top
 Profile  
 
 Post subject: Re: .Net
PostPosted: Wed Apr 15, 2009 2:02 am 
Newbie

Joined: Tue Mar 31, 2009 6:48 am
Posts: 5
santo2 wrote:
Hey guys, I'm also having the same issue.. i'm having really nice objects with lots of collections in it, but when loading a list this is of course not that great. i'm getting quite allot of information that I don't need, and i would like to keep working with Criteria because i'm not support HQL in my methods.
I've built some easy methods where in you can add a list of criteria to get data out of a WCF Service (.Net of course)

By the hand of your explenation of implementing Projection and extending IdentifierProject I actually still don't know what to do..
Can you give a small example please?

Thanks allot

Here you are:

Code:
Criteria criteria = session.createCriteria(MyClass.class);
Projection listProjection = new NoJoinProjection(MyClass.class);
criteria.setProjection(listProjection);
return criteria.list();

And NoJoinProjection is like:

Code:
public class NoJoinProjection extends IdentifierProjection {
   private Class<?> projectionClass;

   public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
      return new Type[] { new ManyToOneType(projectionClass.getName()) };
   }

   public NoJoinProjection(Class<?> projectionClass) {
      super();
      this.projectionClass = projectionClass;
   }

}

The drawback is that all related entities get loaded in separate SQL queries. And all EAGER collections get loaded anyway, but you don't get multiplied number of resultes any more.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 15, 2009 3:36 am 
Beginner
Beginner

Joined: Fri Sep 26, 2008 9:19 am
Posts: 20
Location: Belgium
thanks for the example.

Is this only for relational collections (one to many bags)
or also for many to one? because many to one may be in there for me.

greets


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 15, 2009 11:49 am 
Newbie

Joined: Tue Mar 31, 2009 6:48 am
Posts: 5
For many to one, there's no problem, as there is no collection, right? At least in direction from many to one.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 15, 2009 12:31 pm 
Beginner
Beginner

Joined: Fri Sep 26, 2008 9:19 am
Posts: 20
Location: Belgium
thanks for the answer.

mabe you also know how to solve this:

http://forum.hibernate.org/viewtopic.php?t=996464


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 10:05 am 
Beginner
Beginner

Joined: Fri Sep 26, 2008 9:19 am
Posts: 20
Location: Belgium
Hey,

I'm trying to copy your NoJoinProjection class to C# but i don't have any clue of how to do that.

is there anyone who could translate the NoJoinProjection class into a C# code?

thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 22 posts ]  Go to page Previous  1, 2

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.