MetaDataException:类型" * .CustomerEntity"没有得到增强 - 在使用Spring-JPA和OpenJPA创建行时

时间:2017-01-13 15:03:22

标签: java spring spring-data-jpa h2 openjpa

完整代码为on GitHub

主要代码如下(实体和存储库类被省略):

package org.inthemoon.tests.tryspringjpaplushibernate;

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;

import java.io.File;

/**
 * Created by Dims on 13.01.2017.
 */
public class Main {

   @Configuration
   @Import(Service.class)
   @EnableJpaRepositories("org.inthemoon.tests.tryspringjpaplushibernate")
   public static class _Config {

      @Bean
      File programDirectory() {
         File ans = new File(".");
         return ans;
      }

      @Bean
      BasicDataSource dataSource() {
         BasicDataSource ans = new BasicDataSource();
         ans.setDriverClassName("org.h2.Driver");
         File databasePath = new File(programDirectory(), "data\\tryspringjpaplushibernate");
         ans.setUrl("jdbc:h2:file:" + databasePath.getAbsolutePath());
         return ans;
      }

      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
         LocalContainerEntityManagerFactoryBean ans =
            new LocalContainerEntityManagerFactoryBean();
         ans.setDataSource(dataSource());
         ans.setJpaVendorAdapter(jpaVendorAdapter());
         ans.setPackagesToScan(getClass().getPackage().getName());


         return ans;
      }

      @Bean
      public JpaVendorAdapter jpaVendorAdapter() {
         OpenJpaVendorAdapter ans = new OpenJpaVendorAdapter();
         ans.setShowSql(false);
         ans.setGenerateDdl(true);
         ans.setDatabase(Database.H2);
         return ans;
      }

      @Bean
      public PlatformTransactionManager transactionManager() {
         JpaTransactionManager ans = new JpaTransactionManager();
         ans.setEntityManagerFactory(entityManagerFactory().getObject());
         return ans;
      }

   }

   @Component
   public static class Service {

      private final CustomerRepo customerRepo;

      @Autowired
      public Service(CustomerRepo customerRepo) {
         this.customerRepo = customerRepo;
      }

      public void doSomeOperation() {

         CustomerEntity customer = new CustomerEntity();
         customer.setId(1);
         customer.setNam("New Customer");

         customerRepo.deleteAll();

         customerRepo.save(customer);

      }
   }

   public static void main(String[] args) {

      ApplicationContext context = new AnnotationConfigApplicationContext(_Config.class);

      Service service = context.getBean( Service.class );
      service.doSomeOperation();

   }
}

尝试初始化上下文时,会发生异常

  

使用名称' customerRepo'创建bean时出错:无法解析引用   to bean' jpaMappingContext'设置bean属性时   ' mappingContext'

的根本原因是

Caused by: <openjpa-2.4.2-r422266:1777108 fatal user error> org.apache.openjpa.util.MetaDataException: The type "class org.inthemoon.tests.tryspringjpaplushibernate.CustomerEntity" has not been enhanced.
    at org.apache.openjpa.meta.ClassMetaData.resolveMeta(ClassMetaData.java:1834)
    at org.apache.openjpa.meta.ClassMetaData.resolve(ClassMetaData.java:1808)
    at org.apache.openjpa.meta.MetaDataRepository.processBuffer(MetaDataRepository.java:829)
    at org.apache.openjpa.meta.MetaDataRepository.resolveMeta(MetaDataRepository.java:726)
    ...

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您熟悉OpenJPA的增强功能吗?这个例外似乎只是告诉我们实体没有得到增强。这里可能有一个更大的问题,但我希望你根本没有增强你的实体。如果您在JSE环境中,则可以简单地为运行时提供-javaagent并指向openjpa jar。有关-javaagent或其他类型增强的信息,请查看OpenJPA文档中的增强主题:

http://openjpa.apache.org/builds/2.2.2/apache-openjpa/docs/manual#ref_guide_pc_enhance

谢谢,

Heath Thomann