Hey.
I'm trying to configure cache in main domain applications and I have a small problem (maybe it is a question). I've looked through entire documentation and other sites with tutorials/explanations how second-level cache works in hibernate, but I've found no answers. Here it is...
For example, I have two domain Objects (it's just a simple example, but perfectly describes my problem. In fact my domain objects are a lot more complicated):
Code:
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class Order {
@Id
@GeneratedValue
Long id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "order")
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
List<Item> items;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class Item {
@Id
@GeneratedValue
Long id;
@ManyToOne
@JoinColumn("order_id")
Order order;
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
Now let's say I would like to load all orders and items (to cache) when my application starts up but I don't want to fetch in one query all orders with fetch join, but I want to load all orders without initializing items collection, then in second query load all items. After that I would like to initialize items collections of all orders for example by calling Hibernate.initialize() on each order's collection (it should initialize them by fetching already cached items).
And there appears my problem, because when I try to initialize items collections that way, hibernate doesn't fetch them from cache (I've already loaded all items to cache). It seems that hibernate uses separate objects in cache for collections... (collection-by-collection and entity-by-entity).
Anyone knows how to workaround this problem ?. Any help would be appreciated.