【问题标题】:Error creating bean entityManagerFactory, NoSuchMethodError: javax/persistence/Table.indexes创建 bean entityManagerFactory 时出错,NoSuchMethodError: javax/persistence/Table.indexes
【发布时间】:2017-01-03 17:38:49
【问题描述】:

我使用 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 并使用 EntityManager.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;



       }

【问题讨论】:

    标签: java spring hibernate websphere hsqldb


    【解决方案1】:

    您遇到了以下错误。

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

    此错误表明所需的方法在特定类中不可用。 “indexes()”方法在 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 文件。

    【讨论】:

    • 我尝试了 hibernate.version 4.2.21 但我现在得到了这个错误:“方法 unwrap(Class) 未定义类型 EntityManagerFactory”。你有什么建议?简而言之,我想使用 JPA 2.0 将以下代码调整为休眠
    • unwrap() 方法可能不适用于 hibernate-jpa-2.0-api-1.0.1 jar。另一种获取 Session 对象的方法可以使用 EntityManager.getDelegate() 方法。请在此处查看stackoverflow.com/questions/4148231/…
    • 作为最后的评论家,我在上面添加了我的解决方案,我想知道这是否真的是一个好习惯,因为我有 @Autowired EntityManagerFactory 但我通过“entityManagerFactory.createEntityManager()”创建了 EntityManager 实现。您可以从上面注意到,我取出“@PersistenceContext private EntityManager em”并在需要会话的地方添加了“entityManagerFactory.createEntityManager()”。好吧,我可以告诉你它正在工作,但听起来并不奇怪我自动装配了工厂,并且我使用“创建”方法来实现实际使用?
    猜你喜欢
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    • 2020-07-12
    • 1970-01-01
    • 2018-02-18
    相关资源
    最近更新 更多