HibernateProxyHelper.javaget(T object) that detects whether an object is a HibernateProxy and, if so, unwraps it to the real entity via getHibernateLazyInitializer().getImplementation(). If the object is not a proxy, it returns the object unchanged. This is the simplest and most reusable proxy-unwrapping utility in the XStream+Hibernate bridge layer — used by ProxyIdRefMarshaller for ID-based reference tracking and available for any code that needs to inspect the real object behind a proxy wrapper.HibernateProxyHelper is a pure utility class with a single public static generic method. It has no state, no dependencies (beyond org.hibernate.proxy.HibernateProxy), and no instance methods. The generic type parameter <T> preserves the compile-time type through the unwrap operation, so callers don't need to cast.
public static <T> T get(T object) {
if (object instanceof HibernateProxy) {
HibernateProxy proxy = (HibernateProxy) object;
return (T) proxy.getHibernateLazyInitializer().getImplementation();
}
return object;
}
The logic is straightforward:
instanceof HibernateProxy — the standard way to detect Hibernate proxies, since all Hibernate-generated proxies implement this interface.getHibernateLazyInitializer().getImplementation() which forces proxy initialization (triggers lazy loading) and returns the real entity.getImplementation() call forces proxy initialization. If the entity has not been loaded from the database, this triggers a SQL query. Callers must ensure an active Hibernate session is available, or handle the potential LazyInitializationException.HibernateProxyHelper.get(item) to unwrap proxies before checking the ID reference dictionary and marshalling.persistence.xstream package or elsewhere that needs safe proxy unwrapping.| Class | Approach | Context |
|---|---|---|
| HibernateProxyHelper (this class) | Static utility method get() | General-purpose unwrap for any code |
| HibernateProxyConverter | XStream converter that intercepts before reflection serialization | Integrated into XStream converter chain |
| HibernateProxyXPathMarshaller | Unwraps proxies before XPath reference tracking | Integrated into XStream marshalling strategy |
| HibernateMapper | Resolves proxy's real class name for XML element naming | Integrated into XStream mapper chain |
Each of these solves the proxy problem at a different layer of the XStream architecture. HibernateProxyHelper is the lowest-level, most reusable building block.
(T) cast is unchecked because getImplementation() returns Object. This is safe in practice because Hibernate proxies are always subclasses of the entity type, so the returned object is always assignable to T.null input — null instanceof HibernateProxy evaluates to false, so null is returned unchanged. This is correct behavior.Hibernate.isInitialized() first. It always tries to get the implementation, which forces initialization. This is intentional — the helper is used in contexts where the real object is needed (XML export), not for display purposes.868d6abb7 2025 -> 2026 63081666f Source file headers: 2024-> 2025. b6092df09 Copyright 2023 -> 2024 ab45d51fa Copyright 2001-2022 -> 2001-2023. 5f7ef41b8 Copyright 2021 -> 2022 ceb63e8a1 Source code header: (C) 2001-2021. 7c79f1922 Copyright of source header -> 2020. dd5ca38ac CopyRight of all java file-header updated or created. 9ebb88522 Initial commit