Skip to content

Commit cffabf9

Browse files
committed
some code that only needed reflection in Hibernate5
1 parent 27eb0f5 commit cffabf9

File tree

3 files changed

+13
-82
lines changed

3 files changed

+13
-82
lines changed

hibernate6/src/main/java/com/fasterxml/jackson/datatype/hibernate6/Hibernate6ProxySerializer.java

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import org.hibernate.engine.spi.Mapping;
1010
import org.hibernate.engine.spi.SessionFactoryImplementor;
11-
import org.hibernate.engine.spi.SessionImplementor;
11+
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1212
import org.hibernate.proxy.HibernateProxy;
1313
import org.hibernate.proxy.LazyInitializer;
1414
import org.hibernate.proxy.pojo.BasicLazyInitializer;
@@ -301,47 +301,12 @@ static String getIdentifierPropertyName(LazyInitializer init) {
301301
}
302302
}
303303

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-
*/
313304
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-
330305
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");
306+
final SharedSessionContractImplementor session = init.getSession();
307+
if (session != null) {
308+
SessionFactoryImplementor factory = session.getFactory();
309+
return factory.getIdentifierPropertyName(init.getEntityName());
345310
}
346311
return null;
347312
}

hibernate7/src/main/java/com/fasterxml/jackson/datatype/hibernate7/Hibernate7Module.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.fasterxml.jackson.datatype.hibernate7;
22

33
import org.hibernate.SessionFactory;
4+
import org.hibernate.type.MappingContext;
45

56
import com.fasterxml.jackson.core.Version;
67
import com.fasterxml.jackson.databind.AnnotationIntrospector;
7-
import org.hibernate.type.MappingContext;
88

99
public class Hibernate7Module extends com.fasterxml.jackson.databind.Module
1010
{

hibernate7/src/main/java/com/fasterxml/jackson/datatype/hibernate7/Hibernate7ProxySerializer.java

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88

99
import org.hibernate.engine.spi.SessionFactoryImplementor;
1010
import org.hibernate.engine.spi.SessionImplementor;
11+
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1112
import org.hibernate.proxy.HibernateProxy;
1213
import org.hibernate.proxy.LazyInitializer;
1314
import org.hibernate.proxy.pojo.BasicLazyInitializer;
15+
import org.hibernate.type.MappingContext;
1416

1517
import com.fasterxml.jackson.core.JsonGenerator;
1618
import com.fasterxml.jackson.databind.BeanProperty;
@@ -25,7 +27,6 @@
2527
import com.fasterxml.jackson.databind.util.NameTransformer;
2628

2729
import jakarta.persistence.EntityNotFoundException;
28-
import org.hibernate.type.MappingContext;
2930

3031
/**
3132
* Serializer to use for values proxied using {@link HibernateProxy}.
@@ -300,48 +301,13 @@ static String getIdentifierPropertyName(LazyInitializer init) {
300301
}
301302
}
302303
}
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+
313305
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-
330306
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());
345311
}
346312
return null;
347313
}

0 commit comments

Comments
 (0)