This is not quite true. From the Javadoc, the signature of the relevant methods is:
public Object get(Class clazz, Serializable id) ...
public Object load(Class theClass, Serializable id) ...
(irrelevant parts and other overloaded versions omitted for brevity).
So the entity to be loaded can be of any class, only its identifer needs to be serializable.
Entities indeed net not be serializable, as per this quote from Java Persistence with Hibernate, ch. 13.3.2:
Persistent instances are stored in the second-level cache in a disassembled
form. Think of disassembly as a process a bit like serialization (the algorithm is
much, much faster than Java serialization, however).
So I would guess the identifiers (in the query cache) are also not serialized. I could not find any explanation on why id
is declared Serializable
, and lacking this, I can only guess. The API needs a type for the id
parameter, which should be a common supertype of at least the frequently used identifier types Number
and String
, and apart from Object
, Serializable
is the only choice.