创建bean entityManagerFactory时出错,NoSuchMethodError:javax / persistence / Table.indexes

时间:2016-08-27 01:25:59

标签: java spring hibernate websphere hsqldb

我使用In Memory HSQLDB开发了一个简单的应用程序,它通过Windows在Websphere Liberty Profile 8.5中运行。现在我已经通过z / OS390 Mainframe(Unix)在Websphere上发布了这样的应用程序,我收到以下错误。

据了解,它不应受操作系统的影响,因为它是相同的jar(hsqldb-2.3.2.jar),相同的JDK版本(7)和完全相同的myapp.ear文件。

所以,我直截了当的问题是:在entityManagerFactory创建过程中出现“NoSuchMethodError:javax / persistence / Table.indexes”的原因是什么?

让我的生活更难的是,在我的本地Websphere中部署的完全相同的耳朵不会弹出这样的错误。一个间接的问题可能是,在Unix中运行的内存HSQLDB中有什么技巧吗?我错误地读了日志,这样的错误实际上是由一些错误的Spring配置引起的?我不这么认为,因为完全相同的耳朵在另一个Websphere中运行如上所述。

我已经苦苦挣扎了4天才找到可能的原因,但我不能。任何建议都将受到高度赞赏。

错误日志:

WebSphere non-WLM Dispatch Thread t=009bb7a0¨ ContextLoader - Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory'
 defined in myapp.config.root.TestConfiguration: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax/persistence/Table.indexes()ÝLjavax/persistence/Index;
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java

TestConfiguration.java

@Configuration
@EnableTransactionManagement
public class TestConfiguration {
    @Bean(initMethod = "init")
    public TestDataInitializer initTestData() {
        return new TestDataInitializer();
    }

    @Bean(name = "datasource")
    public DriverManagerDataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(org.hsqldb.jdbcDriver.class.getName());
        dataSource.setUrl("jdbc:hsqldb:mem:mydb");
        dataSource.setUsername("sa");
        dataSource.setPassword("jdbc:hsqldb:mem:mydb");

        System.out.println("Untill here was printed without error");

        return dataSource;

    }

    @Bean(name = "entityManagerFactory")

    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DriverManagerDataSource dataSource) {

        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);
        entityManagerFactoryBean.setPackagesToScan(new String[]{"myapp.model"});

        entityManagerFactoryBean.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
        entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());

        Map<String, Object> jpaProperties = new HashMap<String, Object>();
        jpaProperties.put("hibernate.hbm2ddl.auto", "create");
        jpaProperties.put("hibernate.show_sql", "true");
        jpaProperties.put("hibernate.format_sql", "true");
        jpaProperties.put("hibernate.use_sql_comments", "true");
        entityManagerFactoryBean.setJpaPropertyMap(jpaProperties);
        System.out.println("Untill here was printed without error also");

        return entityManagerFactoryBean;
    }
}

TestDataInitializer

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManagerFactory;
@Component
public class TestDataInitializer {

       @Autowired
       private EntityManagerFactory entityManagerFactory;

       @Autowired
       AnotherModelRepository anotherModelRepository;

       public void init() throws Exception {
              SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);

              Session session = sessionFactory.openSession();
              Transaction transaction = session.beginTransaction();

… few session.persit(mymodel)…

POM:

<properties>

              <java-version>1.7</java-version>

              <org.springframework-version>4.1.3.RELEASE</org.springframework-version>

              <spring-security-version>3.2.5.RELEASE</spring-security-version>

              <hibernate.version>4.3.7.Final</hibernate.version>

              <org.slf4j-version>1.6.1</org.slf4j-version>

              <jackson-version>2.4.4</jackson-version>

              <postgres.driver.version>9.3-1100-jdbc41</postgres.driver.version>

       </properties>





       <dependencies>



              <!-- Spring -->

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-context</artifactId>

                     <version>${org.springframework-version}</version>

                     <exclusions>

                           <!-- Exclude Commons Logging in favor of SLF4j -->

                           <exclusion>

                                  <groupId>commons-logging</groupId>

                                  <artifactId>commons-logging</artifactId>

                           </exclusion>

                     </exclusions>

              </dependency>



              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-webmvc</artifactId>

                     <version>${org.springframework-version}</version>

              </dependency>



              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-tx</artifactId>

                     <version>${org.springframework-version}</version>

              </dependency>



              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-orm</artifactId>

                     <version>${org.springframework-version}</version>

              </dependency>



              <dependency>

                     <groupId>org.postgresql</groupId>

                     <artifactId>postgresql</artifactId>

                     <version>${postgres.driver.version}</version>

              </dependency>



              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-test</artifactId>

                     <version>${org.springframework-version}</version>

              </dependency>



              <!-- Hibernate -->

              <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>



              <!-- Spring security -->

              <dependency>

                     <groupId>org.springframework.security</groupId>

                     <artifactId>spring-security-core</artifactId>

                     <version>${spring-security-version}</version>

              </dependency>



              <dependency>

                     <groupId>org.springframework.security</groupId>

                     <artifactId>spring-security-web</artifactId>

                     <version>${spring-security-version}</version>

              </dependency>



              <dependency>

                     <groupId>org.springframework.security</groupId>

                     <artifactId>spring-security-config</artifactId>

                     <version>${spring-security-version}</version>

              </dependency>



              <dependency>

                     <groupId>com.allanditzel</groupId>

                     <artifactId>spring-security-csrf-token-filter</artifactId>

                     <version>1.1</version>

              </dependency>



              <!-- Logging -->

              <dependency>

                     <groupId>org.slf4j</groupId>

                     <artifactId>slf4j-api</artifactId>

                     <version>${org.slf4j-version}</version>

              </dependency>

              <dependency>

                     <groupId>org.slf4j</groupId>

                     <artifactId>jcl-over-slf4j</artifactId>

                     <version>${org.slf4j-version}</version>

                     <scope>runtime</scope>

              </dependency>

              <dependency>

                     <groupId>org.slf4j</groupId>

                     <artifactId>slf4j-log4j12</artifactId>

                     <version>${org.slf4j-version}</version>

                     <scope>runtime</scope>

              </dependency>

              <dependency>

                     <groupId>log4j</groupId>

                     <artifactId>log4j</artifactId>

                     <version>1.2.16</version>

              </dependency>



              <!-- Jackson JSON Processor -->

              <dependency>

                     <groupId>com.fasterxml.jackson.core</groupId>

                     <artifactId>jackson-databind</artifactId>

                     <version>${jackson-version}</version>

              </dependency>



              <!-- servlet container provided dependencies -->

              <dependency>

                     <groupId>org.apache.tomcat</groupId>

                     <artifactId>tomcat-servlet-api</artifactId>

                     <version>7.0.30</version>

                     <scope>provided</scope>

              </dependency>



              <!-- test dependencies -->

              <dependency>

                     <groupId>com.jayway.jsonpath</groupId>

                     <artifactId>json-path</artifactId>

                     <version>0.8.1</version>

                     <scope>test</scope>

              </dependency>



              <dependency>

                     <groupId>org.apache.commons</groupId>

                     <artifactId>commons-lang3</artifactId>

                     <version>3.3.2</version>

              </dependency>



              <dependency>

                     <groupId>junit</groupId>

                     <artifactId>junit</artifactId>

                     <version>4.12</version>

                     <scope>test</scope>

              </dependency>



              <dependency>

                     <groupId>org.hsqldb</groupId>

                     <artifactId>hsqldb</artifactId>

                     <version>2.3.2</version>

              </dependency>





       </dependencies>



       <build>

              <finalName>my-app</finalName>



              <plugins>

                     <plugin>

                           <groupId>org.apache.maven.plugins</groupId>

                           <artifactId>maven-compiler-plugin</artifactId>

                           <version>2.3.2</version>

                           <configuration>

                                  <source>${java-version}</source>

                                  <target>${java-version}</target>

                           </configuration>

                     </plugin>





                     <plugin>

                           <groupId>org.apache.maven.plugins</groupId>

                           <artifactId>maven-war-plugin</artifactId>

                           <version>2.6</version>

                           <configuration>

                                  <failOnMissingWebXml>false</failOnMissingWebXml>

                           </configuration>

                     </plugin>





              </plugins>

       </build>

</project>

****于2016年8月28日上午2:45添加巴西,圣保罗时区 由于提供了出色的答案,我最终在Windows Websphere 8.5 Liberty Profile中运行我的应用程序,就像在Mainframe z / OS390 Websphere ND 8.5中一样。我在这里添加了我的未来读者解决方案。基本技巧是(1)降级Hibernate以使用JPA2并使用E​​ntityManager.getDelegate()

@Component

public class TestDataInitializer {



       @Autowired

       private EntityManagerFactory entityManagerFactory;



       @Autowired

       AnotherModelRepository anotherModelRepository;



//    I TOOK @PersistenceContext OUT

/*     @PersistenceContext

       private EntityManager em;*/



       public void init() throws Exception {

//           I REPLACED entityManagerFactory.unwrap AND sessionFactory.openSession OUT

              //SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);

//Session session = sessionFactory.openSession();



//            BY createEntityManager AND getDelegate

              EntityManager em = entityManagerFactory.createEntityManager();

              Session session = (Session) em.getDelegate();

              Transaction transaction = session.beginTransaction();



…



@Repository

public class MyModelRepository {

/*     @PersistenceContext

       private EntityManager em;*/





       @Autowired

       private EntityManagerFactory entityManagerFactory;



       public MyModel findMyModelById(Long MyModel) {

              EntityManager em = entityManagerFactory.createEntityManager();

              List<MyModel> MyModels = em.createNamedQuery(MyModel.FIND_BY_ID, MyModel.class).setParameter("MyModelId", MyModel).getResultList();



              return MyModels.size() == 1 ? MyModels.get(0) : null;

       }







       public List<MyModel> findOutByIn(Integer certainId){

              //SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);

              EntityManager em = entityManagerFactory.createEntityManager();

              Session session = (Session) em.getDelegate();



              String query = "select c from … c "

                     + " inner join … o "

                     + " where …;



              //Session session = sessionFactory.openSession();

              Transaction transaction = session.beginTransaction();

              List<MyModel> l = session.createQuery(query).list();



        return l;



       }

1 个答案:

答案 0 :(得分:4)

您收到以下错误。

Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax/persistence/Table.indexes()ÝLjavax/persistence/Index;

此错误表示特定类中没有所需的方法。 “index()”方法可以在JPA 2.1 jar(hibernate-jpa-2.1-api)的“javax.persistence.Table”类中找到。一些如何在你的部署中挑选JPA 2.0 jar(hibernate-jpa-2.0-api),它在Table类中没有这个方法。在您的pom.xml中,您使用的是“hibernate-entitymanager 4.3.7.Final”,它提供了JPA 2.1 jar文件。请在您的服务器中查找JPA 2.0 jars文件,您将收到此错误。