We have an extensive entity model with 100+ entity classes. All the entity classes are subclasses of a single entity superclasses. The shared cache mode has been set to ALL.
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "entities")
public abstract class LongIdEntity {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
@Version
@Column(name = "OPT_LOCK_VERSION")
private Long lockVersion;
etc...
}
An example subclass:
@Entity
@Table(name = "cars")
public class Car extends LongIdEntity { ... }
We'd like to cache all entities in the 2nd level cache. The problem is that only 1 cache region is made for all the entities; named LongIdEntity.
Debugging shows Hibernate did find all the entity classes, but assigns them the same region anyway. Because at SessionFactoryImpl:339 :
String cacheRegionName = cacheRegionPrefix + model.getRootClass().getCacheRegionName();
In our case, the call to model.getRootClass() will always yield "LongIdEntity".
I presume this would indeed cache all the entities, but without any control of eviction. Some classes are very frequent and read-only. So we want to keep them pinned into memory. Some are typically used in a certain time span, etc... Cramming it all into the same cache invalidates it all.
Specifying the region in the annotation has no effect. For example:
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE,region = "cars")
@Entity
@Table(name = "cars")
public class Car extends LongIdEntity { ... }
The weird thing is that only shared cache mode ALL picks up the entity classes. In any other mode no entities are - even when annotated with @Cache and/or @Cacheable. Maybe this is an indication ?
So