gpt4 book ai didi

java - 元数据异常 : The type "*.CustomerEntity" has not been enhanced - whentrying to create row with Spring-JPA and OpenJPA

转载 作者:行者123 更新时间:2023-12-01 09:01:24 25 4
gpt4 key购买 nike

完整代码是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();

}
}

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

Error creating bean with name 'customerRepo': Cannot resolve reference to bean 'jpaMappingContext' while setting bean property '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)
...

我做错了什么?

最佳答案

您熟悉 OpenJPA 中的增强功能吗?这个异常似乎只是告诉我们该实体尚未得到增强。这里可能有一个更大的问题,但我希望你只是没有增强你的实体。如果您处于 JSE 环境中,您只需为运行时提供 -javaagent 并指向 openjpa jar。查看 OpenJPA 文档中的增强主题,了解有关 -javaagent 或其他类型增强的信息:

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

谢谢

希思·托曼

关于java - 元数据异常 : The type "*.CustomerEntity" has not been enhanced - whentrying to create row with Spring-JPA and OpenJPA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41637524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com