Hi,
I'm having a (I think) an unusual behavior of 2nd-level caching when a Set is annotated with @OneToMany.
This is the case:
Main entity, Hotel:
Code:
@Entity
@Table(name = "HOTEL")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, include = "all")
public class Hotel extends AbstractPersistentObjectWithLogicalDeletion {
...
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "HOTEL_OID", nullable = false)
@ForeignKey(name = "FK_PICTURE__HOTEL")
@Index(name = "IDX_PICTURE__HOTEL_OID")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, include = "all")
public Set<Picture> getPictures() {
if (this.pictures == null) {
this.pictures = new HashSet<Picture>();
}
return this.pictures;
}
...
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "HOTEL_PAYMENT_MEAN", joinColumns = @JoinColumn(name = "HOTEL_OID"), inverseJoinColumns = @JoinColumn(name = "PAYMENT_MEAN_OID"))
@ForeignKey(name = "FK_HOTEL__HOTEL_PAYMENT_MEAN", inverseName = "FK_PAYMENT_MEAN__HOTEL_PAYMENT_MEAN")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, include = "all")
public Set<PaymentMean> getPaymentMean() {
if (this.paymentMean == null) {
this.paymentMean = new HashSet<PaymentMean>();
}
return this.paymentMean;
}
...
}
OneToMany child entity, Picture:
Code:
@Entity
@Table(name = "PICTURE")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, include = "all")
public class Picture extends AbstractPersistentObject {
...
}
ManyToMany child entity, PaymentMean:
Code:
@Entity
@Table(name = "PAYMENT_MEAN")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING)
public abstract class PaymentMean extends AbstractPersistentObjectWithLogicalDeletion {
...
}
I'm using a 2nd level cache implementation that stores the data on memcached servers, so if the object gets too dehydrated, it takes up to 106 cache-puts in the memcached (average). The point is that with this approach, enabling memcached as 2nd level duplicates the response time that I get with 2nd level disabled, I mean, going to the db every time.
After I clear the cache and seeing that the keys stored in the cache are the following:
Code:
set> com.despegar.hotel.model.Hotel:0:358
set> com.despegar.hotel.model.Hotel.paymentMean:0:358
set> com.despegar.hotel.model.Picture:0:3531
set> com.despegar.hotel.model.Picture:0:3532
set> com.despegar.hotel.model.Hotel.pictures:0:358
Now, the questions...
- Why is storing the 2 records of paymentMean in one single object and pictures doesn't?
- Is there a Loader / Persister hint/annotation I can set to change this behavior?
- Am I totally wrong and this a expected and unmodifiable behavior?
Thanks in advance!
German Kondolf