-->
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.  [ 4 posts ] 
Author Message
 Post subject: OneToMany EJB3 FetchType.LAZY is ignored
PostPosted: Thu Jan 17, 2008 6:35 am 
Newbie

Joined: Thu Jan 05, 2006 6:03 am
Posts: 9
Hi

i have class Account containing a list of class Tracks:

-------------------
class Account {
private List<Track> tracks;

@OneToMany(mappedBy = "account", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@OrderBy("trackName ASC")
@OnDelete(action=OnDeleteAction.CASCADE)
@Basic(fetch = FetchType.LAZY)
@LazyToOne(LazyToOneOption.PROXY)
@LazyCollection(LazyCollectionOption.TRUE)
public List<Track> getTracks() {
return this.tracks;
}
}
-------------------

a query of type

-------------------
em.createQuery("from Account u where u.loginName = :loginName").
setParameter("loginName", loginName).getSingleResult();
-------------------

directly loads recursive all children and grandchildren (no getters are called),
like this the request takes a minute and the DB is unusable.

Hibernate 3.2 cr2, jboss-EJB-3.0_RC9_Patch_1
PostgreSQL 8.1.5, Linux 2.6.18

How can i prevent loading childs?
Thanks
Marcel


Top
 Profile  
 
 Post subject: Re: OneToMany EJB3 FetchType.LAZY is ignored
PostPosted: Thu Jan 17, 2008 2:04 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Hey Marcel,


I did a similar example and it is working for me. Check if you can find any differences here:


Code:
package test.model.data.jpa;

import javax.persistence.*;


@Entity
@Table(name = "CAR")
public class Car {
    private Long id;
    private String name;
    private Stereo stereo;

    @Id
    @GeneratedValue
    @Column(name = "id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "Name")
    public String getName() {
        return name;
    }

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

    @ManyToOne
    @JoinColumn(name = "STEREO_ID")
    public Stereo getStereo() {
        return stereo;
    }

    public void setStereo(Stereo stereo) {
        this.stereo = stereo;
    }
}


and the other side of relation with the same options you had put except @Basic since it is redundant:

Code:
package test.model.data.jpa;

import org.hibernate.annotations.*;

import javax.persistence.CascadeType;
import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;


@Entity
@Table(name = "STEREO")
public class Stereo {
    private Long id;
    private String name;
    private List<Car> cars = new ArrayList<Car>(2);

    @Id
    @GeneratedValue
    @Column(name = "id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "Name")
    public String getName() {
        return name;
    }

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

    @OneToMany(mappedBy = "stereo", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @OrderBy("name ASC")
    @OnDelete(action = OnDeleteAction.CASCADE)
    @LazyToOne(LazyToOneOption.PROXY)
    @LazyCollection(LazyCollectionOption.TRUE)
    public List<Car> getCars() {
        return cars;
    }

    public void setCars(List<Car> cars) {
        this.cars = cars;
    }
}


and the query is:

Code:
        Query query = em.createQuery("from Stereo s where s.name = :name");

        query.setParameter("name", "JVC 101");

        final Stereo stereo = (Stereo) query.getSingleResult();


It only fetches the stereo and no list of cars afterwards.


Bests,
Farzad-


Top
 Profile  
 
 Post subject: Re: OneToMany EJB3 FetchType.LAZY is ignored (Resolved)
PostPosted: Thu Jan 17, 2008 5:02 pm 
Newbie

Joined: Thu Jan 05, 2006 6:03 am
Posts: 9
Hi Farzad,

thanks for your details and code snippet.

The reason was that i had added in the setter
some lines of code:

/** NO Code in setter, it breaks hibernate! */
public void setTracks(List<Track> tracks) {
this.tracks = tracks;
if (this.tracks == null || this.tracks.size() < 1)
return;
for (Track t : this.tracks)
t.setAccount(this);
}

after changing it to

public void setTracks(List<Track> tracks) {
this.tracks = tracks;
}

the LAZY works as expected,
(this took me two days - oi oi?!)

Thanks
Marcel


Top
 Profile  
 
 Post subject: Re: OneToMany EJB3 FetchType.LAZY is ignored (Resolved)
PostPosted: Thu Jan 17, 2008 5:11 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Marcel,


I do believe hibernate puts a proxied collection in the object and the size method call inside the setter triggers the initialization. That somehow explains the reason.



Farzad--


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