I'm wondering if there is a way to enable second-level cache to work for one-to-one association from "mappedBy" side (or non-owning side). Here is an example:
Appliance and Service are zero/one-to-one relationship, with Service being the owning side and Appliance specifies "mappedBy" clause.
Code snippet:
@Entity(name="Appliance")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class ApplianceImpl {
...
@OneToOne(targetEntity = ServiceImpl.class,
cascade = { CascadeType.ALL }, mappedBy = "appliance")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
private Service service;
...
}
@Entity(name="Service"
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class ServiceImpl {
...
@OneToOne(targetEntity = ApplianceImpl.class,
fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "idAppliance")
private Appliance appliance;
...
}
As you say see, cache is enabled on both entities and 'service' property on Appliance. But every time I use Appliance.service, it still go to DB to fetch the service (I did see both entities got added to second-level cache from the log).
This is causing performance problem to us, since we are referring to Appliance.service extremely frequently. We cannot change Appliance.service to @OneToMany, because there are many places actually search on "Appliance.service" property (so having Hiberate return a collection and return 'service' as a synthetic property won't work). And (to make things worse), lazy loading on Appliance.service won't work, since the one-to-one association is optional.
Any help? I googled quite a bit, and the only relevant info I found is this:
http://www.mail-archive.com/hibernate-d ... 05195.html
But I still don't see why caching the one-to-one association from the non-owning side would be so difficult...
BTW, I'm using Hibernate 3.2.0 and JBoss 4.0.5
Thanks,
-Ting