|
8 | 8 |
|
9 | 9 | import org.hibernate.engine.spi.Mapping; |
10 | 10 | import org.hibernate.engine.spi.SessionFactoryImplementor; |
11 | | -import org.hibernate.engine.spi.SessionImplementor; |
| 11 | +import org.hibernate.engine.spi.SharedSessionContractImplementor; |
12 | 12 | import org.hibernate.proxy.HibernateProxy; |
13 | 13 | import org.hibernate.proxy.LazyInitializer; |
14 | 14 | import org.hibernate.proxy.pojo.BasicLazyInitializer; |
@@ -249,6 +249,7 @@ private String getIdentifierPropertyName(final LazyInitializer init) { |
249 | 249 | if (_mapping != null) { |
250 | 250 | idName = _mapping.getIdentifierPropertyName(init.getEntityName()); |
251 | 251 | } else { |
| 252 | + // no unit tests rely on this next call and Hibernate 7 does not support it |
252 | 253 | idName = ProxySessionReader.getIdentifierPropertyName(init); |
253 | 254 | if (idName == null) { |
254 | 255 | idName = ProxyReader.getIdentifierPropertyName(init); |
@@ -301,47 +302,13 @@ static String getIdentifierPropertyName(LazyInitializer init) { |
301 | 302 | } |
302 | 303 | } |
303 | 304 |
|
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 | | - */ |
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 | | - |
| 306 | + |
330 | 307 | 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"); |
| 308 | + final SharedSessionContractImplementor session = init.getSession(); |
| 309 | + if (session != null) { |
| 310 | + SessionFactoryImplementor factory = session.getFactory(); |
| 311 | + return factory.getIdentifierPropertyName(init.getEntityName()); |
345 | 312 | } |
346 | 313 | return null; |
347 | 314 | } |
|
0 commit comments