Я разработал простое приложение, использующее In Memory HSQLDB, и оно работает в Websphere Liberty Profile 8.5 поверх Windows. Теперь я опубликовал такое приложение в Websphere через z/OS390 Mainframe (Unix), и я получаю ошибку ниже.Ошибка создания bean entityManagerFactory, NoSuchMethodError: javax/persistence/Table.indexes
Насколько это возможно, он не должен зависеть от операционной системы, поскольку он является тем же самым банком (hsqldb-2.3.2.jar), той же версией JDK (7) и точно таким же файлом myapp.ear.
Итак, мой прямой вопрос: что может быть причиной «NoSuchMethodError: javax/persistence/Table.indexes» во время создания entityManagerFactory?
Что делает мою жизнь труднее, так это то же самое ухо, развернутое в моей локальной Websphere, не вызывает такой ошибки. Непрямым может быть вопрос, есть ли какой-либо трюк, который нужно сделать в памяти HSQLDB, запущенной в Unix? Я неправильно читаю журналы, и такая ошибка на самом деле вызвана неправильной конфигурацией 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)…
РОМ:
<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>
**** Добавлено 28 августа 2016 года 2:45 утра Бразилия, Сан-Паулу часовой пояс Благодаря отличным ответам, я наконец получил свое приложение, работающее как в Windows Websphere 8.5 Liberty Profile, так и в Mainframe z/OS390 Websphere ND 8.5. Я добавил здесь свое решение для будущих читателей. Основной трюк был (1) DownGrade 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;
}
Я пробовал hibernate.version 4.2.21, но я получил эту ошибку: «Развертывание метода (Class) не определено для типа EntityManagerFactory». Что ты предлагаешь мне? В нескольких словах я хочу настроить следующий код для спящего режима <4.3.x с JPA 2.0: "public void init() throws Exception {SessionFactory sessionFactory = entityManagerFactory.unwrap (SessionFactory.class); ..." Я думаю, я просто придется менять «разворот» на ??? метод. Постскриптум Я читал об альтернативах, чтобы заставить WEBSphere работать с JPA2.1, но я предпочитаю понижать свою банку, вместо того чтобы менять что-либо в корпоративной сети. –
Метод unrap() может быть недоступен с jibernate-jpa-2.0-api-1.0.1. Другой способ получить объект Session можно использовать метод EntityManager.getDelegate().Пожалуйста, проверьте здесь http://stackoverflow.com/questions/4148231/how-can-i-get-the-session-object-if-i-have-the-entitymanager – abaghel
Как последний критик, я добавил свое решение выше и Мне интересно, действительно ли это хорошая практика, поскольку у меня есть @Autowired EntityManagerFactory, но я создал реализацию EntityManager через «entityManagerFactory.createEntityManager()». Вы можете заметить, что я вынул «@PersistenceContext private EntityManager em» и добавил «entityManagerFactory.createEntityManager()», когда мне нужен сеанс. Ну, я могу сказать вам, что он работает, но не звучит странно. Я автоматически запустил Factory, и я использую метод «create» для реальной реализации. –