-->
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: How can enable Cache in ManyToMany assoication
PostPosted: Fri Feb 02, 2007 3:10 pm 
Newbie

Joined: Fri Feb 02, 2007 6:55 am
Posts: 8
I've try to enable caching. The apps is using 3.2.1.ga

Consider that there are two classes

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class TrafficAlarm
{

private Set<TrafficRecord> records;

@ManyToMany(fetch=FetchType.LAZY)
public Set<TrafficRecord> getRecords() {
return records;
}
public void setRecords(Set<TrafficRecord> records) {
this.records = records;
}

}


@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class FirewallTrafficRecord
{

}


Is it possible to configure the cache such that, when
em.persist(newRecord);
alarm.getRecords().add(newRecord);
the state of the "record" entities will only fetched from the second level cache, OR not ever fetched at all (as the use case shall effective trigger
a one "insert" on the association table)

I have try to use @LazyCollection, @Fetch annonation on the 'getRecords'
properties, but whatever annonation I specify, hibernate still do a join select on the association table. As the collection is very large, and worse it is also accumlating every second, I hope that it is possible to avoid select all records whenever I access the getRecords() properties.

One important is, within the application, I'll only do getRecords().add()
but never iterate the collection or even read the content within.

Another point is, in the real code, both classes are subclass only, but it shouldn't be a problem, right?

Thanks!!!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 03, 2007 8:09 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
@ManyToMany(fetch=FetchType.LAZY)
@Cache(...)
public Set<TrafficRecord> getRecords() {

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 3:43 am 
Newbie

Joined: Fri Feb 02, 2007 6:55 am
Posts: 8
Thanks Emmanuel!

I have digg a little deeper on my problem, as I can see, enable 'cache' on the
huge collection list (property) will not solve my problem.

From the hibernate cache logging message, I can see that, whenever I update the
list
alarm.getRecords().add(newRecord);

Hibernate will lookup the list by a join on the assoc. table and the 'record' entity table.

When persists with entity manager, the cache is invalidated!!!
As a result, caching the collection does nothing.


So, now my question is now becoming two different questions, either one should help me
to overcome the performance problem I have!!

One:
Is it possible to configure hibernate such that, when it want to load the collection,
it only lookup the assocation table (DO NOT JOIN THE OTHER END OF THE RELATION),
then, use the entity id from the assocation table and lookup in the cache.


Two:
Is it LazyCollection(LazyCollectionOption.EXTRA) work on @ManyToMany relation?
If yes, does it have different behavour of 'Set' or 'List' collection.
More important, I have found some other mention that, with lazy="extra" option,
size() method of the collectio would not trigger hibernate to load the complete
collection from DB. However, how about "add()".
In my use case, the collection is huge, but my only operation is "add()".
(ie. the collection is NEVER shrink).

How to configure hibernate so that, when new element add, only 'INSERT' statement
will be issued, but not load collection and then insert.

Is there any special requirement for lazy="extra" to work?
Is it not work for owning side of the association?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 07, 2007 8:03 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
1. you can achieve that with hbm files, not through annotations
2. it should work on many to many but I don't think add() has been made extra lazy. It's probably impossible for Sets, might be doable for list and bags

_________________
Emmanuel


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.