-->
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.  [ 2 posts ] 
Author Message
 Post subject: Manytoone triggers many selects. Or it is not being lazy.
PostPosted: Fri Apr 16, 2010 1:03 pm 
Newbie

Joined: Sun Aug 14, 2005 6:35 pm
Posts: 7
I am using Hibernate 3.3.1.GA

I have three entities
One User has many Answers. Every Answer has zero or one Couple

I get the User ok.
If I get the user.getAnswers(). It starts fetching the Couples and other objects. I don't want that. I only want the Answers.

This is the relevant code.
Code:
@Entity
public class User {
...
    @OneToMany(mappedBy="user")
    @JoinColumn(name="userId")
    @Cascade(value = { CascadeType.ALL, CascadeType.DELETE_ORPHAN })
    @LazyCollection(LazyCollectionOption.TRUE)
    public Set<Answer> getAnswers() {
        if (answers == null)
            answers = new HashSet<Answer>();
        return answers;
    }
...
}

Code:
@Entity
public class Answer {
...
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="coupleId")
    public Couple getCouple() {
        return couple;
    }
...

Code:
@Entity
public class Couple {
...
}

I have also tried with @LazyToOne(LazyToOneOption.PROXY) @Fetch(FetchMode.SELECT)

When I do user.getAnswers() these queries are generated (edited for clarity)
Code:
select * from Answer answers0_  where answers0_.userId=?
select *
  from Couple couple0_ inner join User user1_ on couple0_.manId=user1_.user Id
  left outer join      Photo photo2_ on user1_.approvedPrimaryPhotoId=photo2_.photoId
  left outer join Invitation invitation3_ on user1_.invitationId=invitation3_.invitationId
  left outer join Country country4_ on user1_.countryCode=country4_.countryId
  left outer join PostalZone postalzone5_ on user1_.myPostalCodeId=postalzone5_.postalZoneId
  inner join User user6_ on couple0_.womanId=user6_.userId
  where couple0_.coupleId=?
// repeated and repeated many times.

What I want to do is just to get the Answers without getting the Couples.

_________________
--
Humberto


Top
 Profile  
 
 Post subject: Re: Manytoone triggers many selects. Or it is not being lazy.
PostPosted: Sun Apr 18, 2010 5:00 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
ManyToOne has a well documented limitation: it must check for the "one" part to be !=null.
You can only have it lazily loaded with optional=false which makes the object mandatory, so that it's possible to proxy the object.

_________________
Sanne
http://in.relation.to/


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