创建bean实体管理器工厂时出错,NoSuchMethodError:javax/persistence/Table.indexes [英] Error creating bean entityManagerFactory, NoSuchMethodError: javax/persistence/Table.indexes

查看:121
本文介绍了创建bean实体管理器工厂时出错,NoSuchMethodError:javax/persistence/Table.indexes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用内存中的HSQLDB"开发了一个简单的应用程序,它在Windows上的Websphere Liberty Profile 8.5中运行.现在,我已经在z/OS390大型机(Unix)上的Websphere中发布了此类应用程序,并且出现了以下错误.

I have developed a simple application using In Memory HSQLDB and it runs in Websphere Liberty Profile 8.5 over Windows. Now I have published such application in Websphere over z/OS390 Mainframe (Unix) and I am getting the error below.

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

As far as understand, it should not be affected by the Operation System since it is the same jar (hsqldb-2.3.2.jar), same JDK version (7) and exact same myapp.ear file.

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

So, my straight question is: what could be the reason for "NoSuchMethodError: javax/persistence/Table.indexes" during entityManagerFactory creation?

让我的生活更加艰难的是,部署在本地Websphere中的完全相同的耳朵没有弹出这样的错误.一个间接的问题可能是,要使内存中的HSQLDB在Unix上运行还有什么窍门吗?我是不是在错误地阅读日志,而这种错误实际上是由某些错误的Spring配置引起的?我不这么认为,因为如上所述,完全相同的耳朵在另一个Websphere中运行.

What make my life harder is that the exact same ear deployed in my local Websphere doesn't pop up such error. An indirect question could be, is any trick to make in memory HSQLDB run in Unix? Am I reading wrongly the logs and such error is in fact caused by some wrong Spring config? I don't think so because exact same ear runs in another Websphere as said.

我已经努力4天了,以寻找可能的原因,但是我没有找到.任何建议都会受到高度赞赏.

I have been struggling for 4 days to find a possible reason but I couldn't. Any suggestion will be highly appreciatted.

错误日志:

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

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

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

**** Added in August 28th 2016 2:45am Brazil, São Paulo timezone Thanks to the excellent answers provided, I finaly have my application running in both Windows Websphere 8.5 Liberty Profile as in Mainframe z/OS390 Websphere ND 8.5. I added here my solution for future readers. The basic trick was (1) downgrade Hibernate in order to use JPA2 and use 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;



       }

推荐答案

您遇到以下错误.

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

此错误表明必需的方法在特定类中不可用. JPA 2.1 jar(hibernate-jpa-2.1-api)的"javax.persistence.Table"类中提供了"indexes()"方法.一些在您的部署中如何使用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 jar文件.

This error is indicating that required method is not available in the particular class. The "indexes()" method is available at "javax.persistence.Table" class of JPA 2.1 jar (hibernate-jpa-2.1-api). Some how in your deployment it is picking up JPA 2.0 jar (hibernate-jpa-2.0-api) which doesn't have this method in Table class. In your pom.xml you are using "hibernate-entitymanager 4.3.7.Final", which provides JPA 2.1 jar file. Please look for the JPA 2.0 jars file in your server where you are getting this error.

这篇关于创建bean实体管理器工厂时出错,NoSuchMethodError:javax/persistence/Table.indexes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆