-->
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: How to prevent loading of association
PostPosted: Fri Mar 12, 2010 1:46 pm 
Newbie

Joined: Tue Mar 09, 2010 6:30 pm
Posts: 6
Hi,

I have two classes, Item and Order mapped as One-to-Many such as:

Code:
class Item...

    @OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.MERGE, CascadeType.REMOVE, CascadeType.PERSIST }, targetEntity = OrderImpl.class)
    @JoinColumn(name = "Item_ID", referencedColumnName = "Item_ID", nullable = false, updatable = false, insertable = true)
    @Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
    public Set<Order> getOrders() {
        return myOrders;
    }


My Order class only have an identifier to the Item class, it does not have a getItem(), only a getItemId(). The association is one to many because that is what it is actually like.

I would like to be able to provide Hibernate with a set of Orders that I have previously loaded. I have enabled second level cache, and it works fine the second time around, but on the very first load (ie nothing in the cache), I would like Hibernate not to fetch my Order to populate my collection. Instead, I want to provide it with the ones that I know for sure are linked to it. Is there a certain way to achieve this? My attempts at not using the CascadeType.REFRESH attribute to try and do this have not worked. Would a many-to-many association work better?

Any help would be appreciated.

Regards,

Greg


Top
 Profile  
 
 Post subject: Re: How to prevent loading of association
PostPosted: Mon Mar 15, 2010 4:20 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
I would like Hibernate not to fetch my Order to populate my collection.


When you don't want that Hibernate immediately load's the orders belonging to an item, then you should not
define fetch = FetchType.EAGER on the @OneToMany relation.

Quote:
Instead, I want to provide it with the ones that I know for sure are linked to it.


I don't understand exactly what you mean here.
Do you want to fill the collection partially?


Top
 Profile  
 
 Post subject: Re: How to prevent loading of association
PostPosted: Mon Mar 15, 2010 6:50 am 
Newbie

Joined: Tue Mar 09, 2010 6:30 pm
Posts: 6
Quote:
When you don't want that Hibernate immediately load's the orders belonging to an item, then you should not
define fetch = FetchType.EAGER on the @OneToMany relation.
...
I don't understand exactly what you mean here.
Do you want to fill the collection partially?


No I want to fill the collection fully but I don't want Hibernate to do it. My issue is that when I defined the FetchType as lazy, the collection was not loaded but the moment I was trying to set it manually (ie as soon as getWindows() is called on the Room object), that triggered the load from the DB. Basically I would love to be able to tell Hibernate that I'm giving that collection manually but that it should still be able to: cache it; perform merge/remove/persist operations for me.

But I understand that this might not be possible.

Regards,

Greg


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.