|
8 | 8 |
|
9 | 9 | import org.hibernate.engine.spi.SessionFactoryImplementor; |
10 | 10 | import org.hibernate.engine.spi.SessionImplementor; |
| 11 | +import org.hibernate.engine.spi.SharedSessionContractImplementor; |
11 | 12 | import org.hibernate.proxy.HibernateProxy; |
12 | 13 | import org.hibernate.proxy.LazyInitializer; |
13 | 14 | import org.hibernate.proxy.pojo.BasicLazyInitializer; |
| 15 | +import org.hibernate.type.MappingContext; |
14 | 16 |
|
15 | 17 | import com.fasterxml.jackson.core.JsonGenerator; |
16 | 18 | import com.fasterxml.jackson.databind.BeanProperty; |
|
25 | 27 | import com.fasterxml.jackson.databind.util.NameTransformer; |
26 | 28 |
|
27 | 29 | import jakarta.persistence.EntityNotFoundException; |
28 | | -import org.hibernate.type.MappingContext; |
29 | 30 |
|
30 | 31 | /** |
31 | 32 | * Serializer to use for values proxied using {@link HibernateProxy}. |
@@ -300,48 +301,13 @@ static String getIdentifierPropertyName(LazyInitializer init) { |
300 | 301 | } |
301 | 302 | } |
302 | 303 | } |
303 | | - |
304 | | - /** |
305 | | - * Hibernate 5.2 broke abi compatibility of org.hibernate.proxy.LazyInitializer.getSession() |
306 | | - * The api contract changed |
307 | | - * from org.hibernate.proxy.LazyInitializer.getSession()Lorg.hibernate.engine.spi.SessionImplementor; |
308 | | - * to org.hibernate.proxy.LazyInitializer.getSession()Lorg.hibernate.engine.spi.SharedSessionContractImplementor |
309 | | - * |
310 | | - * On hibernate 5.2 the interface SessionImplementor extends SharedSessionContractImplementor. |
311 | | - * And an instance of org.hibernate.internal.SessionImpl is returned from getSession(). |
312 | | - */ |
| 304 | + |
313 | 305 | protected static class ProxySessionReader { |
314 | | - |
315 | | - /** |
316 | | - * The getSession method must be executed using reflection for compatibility purpose. |
317 | | - * For efficiency keep the method cached. |
318 | | - */ |
319 | | - protected static final Method lazyInitializerGetSessionMethod; |
320 | | - |
321 | | - static { |
322 | | - try { |
323 | | - lazyInitializerGetSessionMethod = LazyInitializer.class.getMethod("getSession"); |
324 | | - } catch (Exception e) { |
325 | | - // should never happen: the class and method exists in all versions of hibernate 5 |
326 | | - throw new RuntimeException(e); |
327 | | - } |
328 | | - } |
329 | | - |
330 | 306 | static String getIdentifierPropertyName(LazyInitializer init) { |
331 | | - final Object session; |
332 | | - try{ |
333 | | - session = lazyInitializerGetSessionMethod.invoke(init); |
334 | | - } catch (Exception e) { |
335 | | - // Should never happen |
336 | | - throw new RuntimeException(e); |
337 | | - } |
338 | | - if(session instanceof SessionImplementor){ |
339 | | - SessionFactoryImplementor factory = ((SessionImplementor)session).getFactory(); |
340 | | - return factory.getIdentifierPropertyName(init.getEntityName()); |
341 | | - }else if (session != null) { |
342 | | - // Should never happen: session should be an instance of org.hibernate.internal.SessionImpl |
343 | | - // factory = session.getClass().getMethod("getFactory").invoke(session); |
344 | | - throw new RuntimeException("Session is not instance of SessionImplementor"); |
| 307 | + final SharedSessionContractImplementor session = init.getSession(); |
| 308 | + if (session != null) { |
| 309 | + SessionFactoryImplementor factory = session.getFactory(); |
| 310 | + return factory.getIdentifierPropertyName(init.getEntityName()); |
345 | 311 | } |
346 | 312 | return null; |
347 | 313 | } |
|
0 commit comments