Skip to content

Update hibernate 6 code to avoid deprecated code #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import org.hibernate.engine.spi.Mapping;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;
import org.hibernate.proxy.pojo.BasicLazyInitializer;
Expand Down Expand Up @@ -249,6 +249,7 @@ private String getIdentifierPropertyName(final LazyInitializer init) {
if (_mapping != null) {
idName = _mapping.getIdentifierPropertyName(init.getEntityName());
} else {
// no unit tests rely on this next call and Hibernate 7 does not support it
idName = ProxySessionReader.getIdentifierPropertyName(init);
if (idName == null) {
idName = ProxyReader.getIdentifierPropertyName(init);
Expand Down Expand Up @@ -301,47 +302,13 @@ static String getIdentifierPropertyName(LazyInitializer init) {
}
}

/**
* Hibernate 5.2 broke abi compatibility of org.hibernate.proxy.LazyInitializer.getSession()
* The api contract changed
* from org.hibernate.proxy.LazyInitializer.getSession()Lorg.hibernate.engine.spi.SessionImplementor;
* to org.hibernate.proxy.LazyInitializer.getSession()Lorg.hibernate.engine.spi.SharedSessionContractImplementor
*
* On hibernate 5.2 the interface SessionImplementor extends SharedSessionContractImplementor.
* And an instance of org.hibernate.internal.SessionImpl is returned from getSession().
*/
protected static class ProxySessionReader {

/**
* The getSession method must be executed using reflection for compatibility purpose.
* For efficiency keep the method cached.
*/
protected static final Method lazyInitializerGetSessionMethod;

static {
try {
lazyInitializerGetSessionMethod = LazyInitializer.class.getMethod("getSession");
} catch (Exception e) {
// should never happen: the class and method exists in all versions of hibernate 5
throw new RuntimeException(e);
}
}


static String getIdentifierPropertyName(LazyInitializer init) {
final Object session;
try{
session = lazyInitializerGetSessionMethod.invoke(init);
} catch (Exception e) {
// Should never happen
throw new RuntimeException(e);
}
if(session instanceof SessionImplementor){
SessionFactoryImplementor factory = ((SessionImplementor)session).getFactory();
return factory.getIdentifierPropertyName(init.getEntityName());
}else if (session != null) {
// Should never happen: session should be an instance of org.hibernate.internal.SessionImpl
// factory = session.getClass().getMethod("getFactory").invoke(session);
throw new RuntimeException("Session is not instance of SessionImplementor");
final SharedSessionContractImplementor session = init.getSession();
if (session != null) {
SessionFactoryImplementor factory = session.getFactory();
return factory.getIdentifierPropertyName(init.getEntityName());
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ private Session openTemporarySessionForLoading(PersistentCollection coll) {
session.setHibernateFlushMode(FlushMode.MANUAL);

persistenceContext.addUninitializedDetachedCollection(
((SessionFactoryImplementor) _sessionFactory).getMetamodel().collectionPersister(coll.getRole()),
coll
((SessionFactoryImplementor) _sessionFactory).getMappingMetamodel().getCollectionDescriptor(coll.getRole()),
coll
);

return session;
Expand Down
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Project: jackson-datatype-hibernate

2.20.0 (not yet released)

#186: Update hibernate 6 code to avoid deprecated code
(fixed by @pjfanning)
- Generate SBOMs [JSTEP-14]

2.19.1 (13-Jun-2025)
Expand Down
Loading