-->
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.  [ 5 posts ] 
Author Message
 Post subject: Is possible to change runtime lazy query transaction as eage
PostPosted: Thu Feb 07, 2008 7:21 am 
Newbie

Joined: Tue Dec 18, 2007 9:15 am
Posts: 14
By default hibernate works in lazy mode transaction. Sometimes I need to extract all related objects in eager mode, but I don´t want to declare it as a annotation (my annotation is in lazy mode). Can I sometimes bring these datas as eager? How?

Thanks!

_________________
-= DUNKELHEIT =-


Top
 Profile  
 
 Post subject: Re: Is possible to change runtime lazy query transaction as
PostPosted: Thu Feb 07, 2008 8:48 am 
Regular
Regular

Joined: Mon Aug 20, 2007 6:47 am
Posts: 74
Location: UK
Grinvon wrote:
By default hibernate works in lazy mode transaction. Sometimes I need to extract all related objects in eager mode, but I don´t want to declare it as a annotation (my annotation is in lazy mode). Can I sometimes bring these datas as eager? How?

Thanks!


I don't think there's anything in hibernate to do this, but you could always call size() on the collection to force a load.


Top
 Profile  
 
 Post subject: Re: Is possible to change runtime lazy query transaction as
PostPosted: Thu Feb 07, 2008 10:12 am 
Newbie

Joined: Tue Dec 18, 2007 9:15 am
Posts: 14
Aye. We can use size() and others methods. But there is a problem. I want that hibernate do not know the collection name.

If I have Entity 1 -> Entity 2 -> Entity 3 - Entity 4.

I want to bring Entity 1 instance, and when I will persist in another EM, it knows all related objects from Entity 1. So Entity 1 brings all related objects with him.

I used NamedQuery to do it, but it don´t appear to be generic, I want a generic soluction for any case to simulate a onfly eager fetch mode.

_________________
-= DUNKELHEIT =-


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 07, 2008 1:55 pm 
Newbie

Joined: Tue Dec 18, 2007 9:15 am
Posts: 14
I have used this solution for my issue.

All my entities have a commun superclass.

This superclass have one public abstract method that all subclass need to implement.

See:

Code:
@Transient
public Collection all;

public abstract Collection getAll();



All subclass implements getAll. This method add all dependent object from one entity to a collection.

When I persist Entity 1, re gets all related objects.

For example I have:

Entity 1 class:

Code:
@OneToMany(cascade=CascadeType.ALL, mappedBy="entity1",fetch=FetchType.LAZY)
private List<Entity2> entities2;

    @Override
    public Collection getAll() {
        if (all == null)
            all = new ArrayList();
       
        if (entities2 != null && entities2.size() > 0) {
            for (Entity2 et2 : entities2) {
                all.add(et2);
                et2.getAll();
            }
        }
        return all;
    }


In Entity 2 class:

Code:
    @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    private Entity1 entity1;
   
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    private List<Entity3> entities3;

    @Override
    public Collection getAll() {
        if (all == null)
            all = new ArrayList();
       
        if (entities3 != null && entities3.size() > 0) {
            for (Entity3 et3 : entities3) {
                all.add(et3);
                et3.getAll();
            }
        }
        return all;
    }


And so on...

Thus I can handle all instances, all dependents objects. This is not the most clear and elegant solution, but this is the solution that I thought. I hope I was clear, cuz my english is not so good.

If someone else have another solution, please take a time and post here, will help others. Thanks all.

_________________
-= DUNKELHEIT =-


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 08, 2008 8:21 am 
Newbie

Joined: Tue Dec 18, 2007 9:15 am
Posts: 14
I think in actual state, JPA 1.0 do not support runtime changes about FetchMode. I hope that 2.0 can do it as well and improve "new" things that hibernate have been supported.

_________________
-= DUNKELHEIT =-


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.