Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jeffsheets/aec3e94870ef903ce7efe33e00563d3c to your computer and use it in GitHub Desktop.
Save jeffsheets/aec3e94870ef903ce7efe33e00563d3c to your computer and use it in GitHub Desktop.
Use JPA 2.1 and Hibernate 4.3.11 on Websphere 8.5.5.x
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import javax.persistence.spi.PersistenceProvider;
import javax.persistence.spi.PersistenceProviderResolver;
import javax.persistence.spi.PersistenceProviderResolverHolder;
import java.util.Collections;
import java.util.List;
/**
* This allows to deploy upgraded JPA and Hibernate versions to Websphere 8.5.5.x servers.
* Has been used successfully with JPA 2.1 and Hibernate 4.3.11
*
* Mostly a copy of <a href="https://hibernate.atlassian.net/browse/JPA-4">https://hibernate.atlassian.net/browse/JPA-4</a>
* Changes made to deploy in a Spring environment.
*
* To use: add a @DependsOn("hibernatePersistenceProviderResolver") annotation on a DatabaseConfig class
*/
@Configuration
public class HibernatePersistenceProviderResolver implements PersistenceProviderResolver {
private volatile PersistenceProvider persistenceProvider = new HibernatePersistenceProvider();
@Override
public List<PersistenceProvider> getPersistenceProviders() {
return Collections.singletonList(persistenceProvider);
}
@Override
public void clearCachedProviders() {
persistenceProvider = new HibernatePersistenceProvider();
}
@PostConstruct
public void register() {
PersistenceProviderResolverHolder.setPersistenceProviderResolver(new HibernatePersistenceProviderResolver());
}
}
@sdolman
Copy link

sdolman commented Jun 22, 2017

Which is more appropriate here: @Configuration or @Component? With @Component, is it necessary to also use @Autowired ? (Some information that led to the questions http://dimafeng.com/2015/08/29/spring-configuration_vs_component/ )
Thank you.

@jeffsheets
Copy link
Author

@sdolman great question, thanks! It has been a long time since I used this and I don't have the specific project code anymore. @Component might work, but I'm not sure since this is used as a trick by calling it in the @DependsOn annotation in a DatabaseConfig class. I'm definitely curious though. Did it work for you?

@Prithal
Copy link

Prithal commented Mar 30, 2018

How can i configured this class from spring xml based config?Any idea

@jeffsheets
Copy link
Author

@Prithal, sorry but I haven't done spring xml config in a long time. I'm not sure exactly, but somehow you would need to run the register() method in a PostConstruct, and have the config depend on hibernatePersistenceProviderResolver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment