1

I know this might be a repeated question, but I could not make it work even after referring few of the articles here and there. I am trying to deploy a simple application in websphere which uses spring boot latest version. As the spring boot is using JPA 2.1 hibernate jars, they are colliding with the JPA 2.0 jars which comes with websphere 8.5.0.1. To make this work, I had to exclude the hibernate jars from my spring boot data maven entry and specify a lower version of hibernate. Although this has worked, I am not satisfied since I could not make use of the latest hibernate implementation. So tried to make hibernate latest(JPA 2.1) as the third party provider in websphere. This is what I have tried so far: 1. Environment -> Shared Library -> Created a new SharedLibrary -> Gave the classpath as your c:/app/lib 2. WebApplication Server -> my Server -> Java and Process Management -> Class Loader -> New - Classes loaded with application class loader first -> Added the shared library created in the previous step.

Under Server -> Class Loader is Single and Parent at last is selected. I am getting the below exception when I try to start the application:

Caused by: java.lang.LinkageError: loading constraint violation: loader "com/ibm/ws/classloader/CompoundClassLoader@da74f152" previously initiated loading for a different type with name "org/xml/sax/Locator" defined by loader "com/ibm/oti/vm/BootstrapClassLoader@53942f80"
[11/24/15 9:26:14:812 EST] 00000056 SystemErr     R     at java.lang.ClassLoader.defineClassImpl(Native Method)
[11/24/15 9:26:14:812 EST] 00000056 SystemErr     R     at java.lang.ClassLoader.defineClass(ClassLoader.java:286)
[11/24/15 9:26:14:812 EST] 00000056 SystemErr     R     at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:154)
[11/24/15 9:26:14:812 EST] 00000056 SystemErr     R     at com.ibm.ws.classloader.CompoundClassLoader._defineClass(CompoundClassLoader.java:853)
[11/24/15 9:26:14:812 EST] 00000056 SystemErr     R     at com.ibm.ws.classloader.CompoundClassLoader.localFindClass(CompoundClassLoader.java:763)
[11/24/15 9:26:14:812 EST] 00000056 SystemErr     R     at com.ibm.ws.classloader.CompoundClassLoader.loadClass(CompoundClassLoader.java:604)
[11/24/15 9:26:14:812 EST] 00000056 SystemErr     R     at java.lang.ClassLoader.loadClass(ClassLoader.java:660)
[11/24/15 9:26:14:812 EST] 00000056 SystemErr     R     at java.lang.J9VMInternals.verifyImpl(Native Method)
[11/24/15 9:26:14:812 EST] 00000056 SystemErr     R     at java.lang.J9VMInternals.verify(J9VMInternals.java:85)
[11/24/15 9:26:14:812 EST] 00000056 SystemErr     R     at java.lang.J9VMInternals.verify(J9VMInternals.java:83)
[11/24/15 9:26:14:812 EST] 00000056 SystemErr     R     at java.lang.J9VMInternals.initialize(J9VMInternals.java:162)
[11/24/15 9:26:14:812 EST] 00000056 SystemErr     R     at ch.qos.logback.core.joran.event.SaxEventRecorder.startElement(SaxEventRecorder.java:109)
[11/24/15 9:26:14:812 EST] 00000056 SystemErr     R     at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
[11/24/15 9:26:14:813 EST] 00000056 SystemErr     R     at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
[11/24/15 9:26:14:813 EST] 00000056 SystemErr     R     at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
[11/24/15 9:26:14:813 EST] 00000056 SystemErr     R     at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
[11/24/15 9:26:14:813 EST] 00000056 SystemErr     R     at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
[11/24/15 9:26:14:813 EST] 00000056 SystemErr     R     at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
[11/24/15 9:26:14:813 EST] 00000056 SystemErr     R     at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
[11/24/15 9:26:14:813 EST] 00000056 SystemErr     R     at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
[11/24/15 9:26:14:813 EST] 00000056 SystemErr     R     at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
[11/24/15 9:26:14:813 EST] 00000056 SystemErr     R     at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
[11/24/15 9:26:14:813 EST] 00000056 SystemErr     R     at org.apache.xerces.jaxp.SAXParserImpl.parse(Unknown Source)
[11/24/15 9:26:14:813 EST] 00000056 SystemErr     R     at ch.qos.logback.core.joran.event.SaxEventRecorder.recordEvents(SaxEventRecorder.java:61)
[11/24/15 9:26:14:814 EST] 00000056 SystemErr     R     at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:134)
[11/24/15 9:26:14:814 EST] 00000056 SystemErr     R     at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:99)
[11/24/15 9:26:14:814 EST] 00000056 SystemErr     R     at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:49)
[11/24/15 9:26:14:814 EST] 00000056 SystemErr     R     at ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:77)
[11/24/15 9:26:14:814 EST] 00000056 SystemErr     R     at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:119)
[11/24/15 9:26:14:814 EST] 00000056 SystemErr     R     at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:57)
[11/24/15 9:26:14:814 EST] 00000056 SystemErr     R     at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:98)
[11/24/15 9:26:14:814 EST] 00000056 SystemErr     R     at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:217)
[11/24/15 9:26:14:814 EST] 00000056 SystemErr     R     at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:185)
[11/24/15 9:26:14:814 EST] 00000056 SystemErr     R     at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:164)
[11/24/15 9:26:14:814 EST] 00000056 SystemErr     R     at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:144)
[11/24/15 9:26:14:815 EST] 00000056 SystemErr     R     at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:151)
[11/24/15 9:26:14:815 EST] 00000056 SystemErr     R     at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:128)
[11/24/15 9:26:14:815 EST] 00000056 SystemErr     R     at org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:100)
[11/24/15 9:26:14:815 EST] 00000056 SystemErr     R     at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:59)
[11/24/15 9:26:14:815 EST] 00000056 SystemErr     R     at org.springframework.boot.SpringApplication.run(SpringApplication.java:286)
[11/24/15 9:26:14:815 EST] 00000056 SystemErr     R     at org.springframework.boot.context.web.SpringBootServletInitializer.run(SpringBootServletInitializer.java:134)
[11/24/15 9:26:14:815 EST] 00000056 SystemErr     R     at org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:125)
[11/24/15 9:26:14:815 EST] 00000056 SystemErr     R     at org.springframework.boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:81)
[11/24/15 9:26:14:815 EST] 00000056 SystemErr     R     at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)
[11/24/15 9:26:14:815 EST] 00000056 SystemErr     R     at com.ibm.ws.webcontainer.webapp.WebAppImpl.initializeServletContainerInitializers(WebAppImpl.java:613)
[11/24/15 9:26:14:815 EST] 00000056 SystemErr     R     at com.ibm.ws.webcontainer.webapp.WebAppImpl.initialize(WebAppImpl.java:409)
[11/24/15 9:26:14:815 EST] 00000056 SystemErr     R     at com.ibm.ws.webcontainer.webapp.WebGroupImpl.addWebApplication(WebGroupImpl.java:88)
[11/24/15 9:26:14:815 EST] 00000056 SystemErr     R     at com.ibm.ws.webcontainer.VirtualHostImpl.addWebApplication(VirtualHostImpl.java:169)
[11/24/15 9:26:14:816 EST] 00000056 SystemErr     R     ... 96 more

I have also played around with the default JPA implementor setting and tried putting "org.hibernate.jpa.HibernatePersistenceProvider" as the provider. Then I even tried putting the persistence.xml in META-INF with the provider as "org.hibernate.jpa.HibernatePersistenceProvider". But nothing has worked so far.

The exception I mentioned above comes when I change the class loading option as parent_last for the application. If parent first is chose, it throws the following exception:

java.lang.NullPointerException
[11/24/15 9:24:12:794 EST] 00000055 SystemErr     R     at com.ibm.ws.webcontainer.metadata.WebCollaboratorComponentMetaDataImpl.getJ2EEName(WebCollaboratorComponentMetaDataImpl.java:63)

Kindly let me know if it is at all possible what I am trying.If yes, can anyone guide me with the detailed steps?

UPDATE: Below is my configuration for JPA

@Configuration
@EnableTransactionManagement
@PropertySource(value = { ApplicationConstant.DATABASE_PROPERTIES })
public class HibernateConfiguration {

    /**
     *The variable to store the Environment instance variable
     */
    @Autowired
    private Environment environment;

    /**
     * Bean definition to configure Hibernate SessionFactory
     * @return LocalSessionFactoryBean
     */
   /* @Bean
    public LocalSessionFactoryBean sessionFactory() {
        final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();

        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { ApplicationConstant.PACKAGE_TO_SCAN });
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
     }*/

    /**
     * Bean definition to configure datasource for Hibernate SessionFactory
     * @return DataSource
     */
    @Bean
    public DataSource dataSource() {
        JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
        jndiObjectFactoryBean.setJndiName("jdbc/prsds");
        JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
        DataSource dataSource = dataSourceLookup.getDataSource("jdbc/prsds");
        return dataSource;

        /*
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getRequiredProperty(ApplicationConstant.JDBC_DRIVER_CLASSNAME));
        dataSource.setUrl(environment.getRequiredProperty(ApplicationConstant.JDBC_URL));
        dataSource.setUsername(environment.getRequiredProperty(ApplicationConstant.JDBC_USERNAME));
        dataSource.setPassword(environment.getRequiredProperty(ApplicationConstant.JDBC_PASSWORD));
        return dataSource;*/
    }

    /**
     * This method is used to set the properties for Hibernate configuration
     * @return Properties
     */
    private Properties hibernateProperties() {
        final Properties properties = new Properties();
        properties.put(ApplicationConstant.HIBERNATE_DIALECT, environment.getRequiredProperty(ApplicationConstant.HIBERNATE_DIALECT));
        properties.put(ApplicationConstant.HIBERNATE_SHOW_SQL, environment.getRequiredProperty(ApplicationConstant.HIBERNATE_SHOW_SQL));
        properties.put(ApplicationConstant.HIBERNATE_FORMAT_SQL, environment.getRequiredProperty(ApplicationConstant.HIBERNATE_FORMAT_SQL));
        return properties;
    }

    /**
     * Bean definition to configure Hibernate Transaction manager
     * @param SessionFactory sessionFactory
     * @return
     */
    /*@Bean
    @Autowired
    public HibernateTransactionManager transactionManager(final SessionFactory sessionFactory) {
       final HibernateTransactionManager txManager = new HibernateTransactionManager();
       txManager.setSessionFactory(sessionFactory);
       return txManager;
    }*/
//  ///////////////////////////////////////////////////

     @Bean
       public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
          LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
          em.setDataSource(dataSource());
          em.setPackagesToScan(new String[] { ApplicationConstant.PACKAGE_TO_SCAN});
         // em.setPersistenceUnitName("txManager");
          JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          em.setJpaVendorAdapter(vendorAdapter);
          em.setJpaProperties(hibernateProperties());

          return em;
       }


       @Bean
       public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(emf);

          return transactionManager;
       }

       @Bean
       public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
          return new PersistenceExceptionTranslationPostProcessor();
       }

    }

POM.XML

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.7.RELEASE</version>
    </parent>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <id>xjc</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <schemaDirectory>${project.basedir}/src/main/resources/Service_Defination/XSD</schemaDirectory>
                    <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
                    <clearOutputDir>false</clearOutputDir>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-ws</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.ws.xmlschema</groupId>
            <artifactId>xmlschema-core</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc14</artifactId>
            <version>10.2.0.4.0</version>
        </dependency>
    </dependencies>
</project>

I have also tried with and without persistence.xml but that also did not help. This was the configuration I tried:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence 
            http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.0">
   <persistence-unit name="txManager">
   <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
  <properties>
   <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory"></property>
     <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup"></property>
   </properties>
   </persistence-unit>
</persistence>

Kindly guide me where I am going wrong.

AC_1985
  • 183
  • 9
  • 22

2 Answers2

1

You cannot use container managed JPA-2.1 with classic WebSphere 8.5.x. as it only supports 2.0. It has been already discussed in various post e.g.here. If you really have to use JPA 2.1, you can either use WebSphere Liberty, which supports that or use application managed JPA and create entity manager by yourself not by injection.

UPDATE

I dont know how to do it in spring-boot, but you should use the following code to initialize EntityManager:

EntityManagerFactory emf =
Persistence.createEntityManagerFactory("persistenceUnitName");
EntityManager em = emf.createEntityManager();
Community
  • 1
  • 1
Gas
  • 17,601
  • 4
  • 46
  • 93
  • I understand that and as you have suggested, I was trying to create the entity manager myself. Please see my updates in the question where I have included the sample code. Please let me know what I am doing wrong. – AC_1985 Nov 25 '15 at 08:25
0

I was able to use JPA 2.1 and Hibernate 4.3.11 by using a custom PersistenceProviderResolver so that the Websphere classes do not interfere with Hibernate https://gist.github.com/jeffsheets/aec3e94870ef903ce7efe33e00563d3c

I based it off of a post found here https://hibernate.atlassian.net/browse/JPA-4

Jeff Sheets
  • 1,181
  • 12
  • 18