Skip to content

Commit a96e028

Browse files
committed
revert hibernate 6 changes
1 parent ace4e29 commit a96e028

File tree

3 files changed

+58
-24
lines changed

3 files changed

+58
-24
lines changed

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

Lines changed: 40 additions & 6 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.SharedSessionContractImplementor;
11+
import org.hibernate.engine.spi.SessionImplementor;
1212
import org.hibernate.proxy.HibernateProxy;
1313
import org.hibernate.proxy.LazyInitializer;
1414
import org.hibernate.proxy.pojo.BasicLazyInitializer;
@@ -249,7 +249,6 @@ private String getIdentifierPropertyName(final LazyInitializer init) {
249249
if (_mapping != null) {
250250
idName = _mapping.getIdentifierPropertyName(init.getEntityName());
251251
} else {
252-
// no unit tests rely on this next call and Hibernate 7 does not support it
253252
idName = ProxySessionReader.getIdentifierPropertyName(init);
254253
if (idName == null) {
255254
idName = ProxyReader.getIdentifierPropertyName(init);
@@ -302,12 +301,47 @@ static String getIdentifierPropertyName(LazyInitializer init) {
302301
}
303302
}
304303

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+
*/
305313
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+
306330
static String getIdentifierPropertyName(LazyInitializer init) {
307-
final SharedSessionContractImplementor session = init.getSession();
308-
if (session != null) {
309-
SessionFactoryImplementor factory = session.getFactory();
310-
return factory.getIdentifierPropertyName(init.getEntityName());
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");
311345
}
312346
return null;
313347
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ private Session openTemporarySessionForLoading(PersistentCollection coll) {
307307
session.setHibernateFlushMode(FlushMode.MANUAL);
308308

309309
persistenceContext.addUninitializedDetachedCollection(
310-
((SessionFactoryImplementor) _sessionFactory).getMappingMetamodel().getCollectionDescriptor(coll.getRole()),
310+
((SessionFactoryImplementor) _sessionFactory).getMetamodel().collectionPersister(coll.getRole()),
311311
coll
312312
);
313313

hibernate6/src/test/resources/META-INF/persistence.xml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
66

77
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
8-
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
9-
<class>data.com.fasterxml.jackson.datatype.hibernate6.Customer</class>
10-
<class>data.com.fasterxml.jackson.datatype.hibernate6.Employee</class>
11-
<class>data.com.fasterxml.jackson.datatype.hibernate6.Office</class>
12-
<class>data.com.fasterxml.jackson.datatype.hibernate6.Order</class>
13-
<class>data.com.fasterxml.jackson.datatype.hibernate6.OrderDetail</class>
14-
<class>data.com.fasterxml.jackson.datatype.hibernate6.OrderDetailId</class>
15-
<class>data.com.fasterxml.jackson.datatype.hibernate6.Payment</class>
16-
<class>data.com.fasterxml.jackson.datatype.hibernate6.PaymentId</class>
17-
<class>data.com.fasterxml.jackson.datatype.hibernate6.Product</class>
18-
<properties>
19-
<property name="jakarta.persistence.jdbc.driver" value="org.h2.Driver"/>
20-
<property name="jakarta.persistence.jdbc.user" value=""/>
21-
<property name="jakarta.persistence.jdbc.password" value=""/>
22-
<property name="jakarta.persistence.jdbc.url" value="jdbc:h2:mem:;DB_CLOSE_DELAY=-1;INIT=RUNSCRIPT FROM 'classpath:/classicmodels.sql'"/>
23-
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
24-
</properties>
8+
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
9+
<class>data.com.fasterxml.jackson.datatype.hibernate6.Customer</class>
10+
<class>data.com.fasterxml.jackson.datatype.hibernate6.Employee</class>
11+
<class>data.com.fasterxml.jackson.datatype.hibernate6.Office</class>
12+
<class>data.com.fasterxml.jackson.datatype.hibernate6.Order</class>
13+
<class>data.com.fasterxml.jackson.datatype.hibernate6.OrderDetail</class>
14+
<class>data.com.fasterxml.jackson.datatype.hibernate6.OrderDetailId</class>
15+
<class>data.com.fasterxml.jackson.datatype.hibernate6.Payment</class>
16+
<class>data.com.fasterxml.jackson.datatype.hibernate6.PaymentId</class>
17+
<class>data.com.fasterxml.jackson.datatype.hibernate6.Product</class>
18+
<properties>
19+
<property name="jakarta.persistence.jdbc.driver" value="org.h2.Driver"/>
20+
<property name="jakarta.persistence.jdbc.user" value=""/>
21+
<property name="jakarta.persistence.jdbc.password" value=""/>
22+
<property name="jakarta.persistence.jdbc.url" value="jdbc:h2:mem:;DB_CLOSE_DELAY=-1;INIT=RUNSCRIPT FROM 'classpath:/classicmodels.sql'"/>
23+
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
24+
</properties>
2525
</persistence-unit>
2626

2727
</persistence>

0 commit comments

Comments
 (0)