Spring Data JPA(openjpa) 报错:has not been enhanced

发布于 2021-11-29 03:37:29 字数 8237 浏览 854 评论 2

直接使用openjpa没有问题

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="2.0">

    <persistence-unit name="myjpa" transaction-type="RESOURCE_LOCAL">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <class>com.pp.jpa.openjpa.User</class>
        <properties>
            <property name="openjpa.ConnectionURL" value="jdbc:mysql:///jpa"/>
            <property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
            <property name="openjpa.ConnectionUserName" value="root"/>
            <property name="openjpa.ConnectionPassword"  value="admin123"/>
        </properties>
    </persistence-unit>
    
</persistence>



<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.40</version>
		</dependency>
		<dependency>
			<groupId>org.apache.openjpa</groupId>
			<artifactId>openjpa-all</artifactId>
			<version>2.4.1</version>
		</dependency>



使用spring data jpa整合openjpa就报错:
<dependency>
	<groupId>org.springframework.data</groupId>
	<artifactId>spring-data-jpa</artifactId>
	<version>1.10.4.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.apache.openjpa</groupId>
	<artifactId>openjpa-all</artifactId>
	<version>2.4.1</version>
</dependency>



package com.pp.jpa.spring.openjpa;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

@Configuration
public class JpaConfig {

	@Bean
	public DataSource createDataSource() {
		MysqlDataSource ds = new MysqlDataSource();
		ds.setURL("jdbc:mysql:///jpa");
		ds.setUser("root");
		ds.setPassword("admin123");
		return ds;
	}

	@Bean
	public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
		OpenJpaVendorAdapter vendorAdapter = new OpenJpaVendorAdapter();
		vendorAdapter.setShowSql(true);
		LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
		factory.setJpaVendorAdapter(vendorAdapter);
		factory.setPackagesToScan("com.pp.jpa.spring.openjpa");
		factory.setDataSource(dataSource);
		return factory;
	}

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


package com.pp.jpa.spring.openjpa;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="user2")
public class User {
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer uid;
	
	@Column
	private String name;
	
	public Integer getUid() {
		return uid;
	}
	public void setUid(Integer uid) {
		this.uid = uid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String toString() {
		return "User [uid=" + uid + ", name=" + name + "]";
	}
	public User(String name) {
		this.name = name;
	}
	public User(){}
}



package com.pp.jpa.spring.openjpa;

import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository<User, Integer> {
	
}


package com.pp.jpa.spring.openjpa;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement
@EnableJpaRepositories
@ComponentScan
public class App {
    public static void main( String[] args ) {
    	AnnotationConfigApplicationContext context = null;
		context = new AnnotationConfigApplicationContext(App.class);
		System.out.println(context.getBean(UserRepository.class));
		context.close();
    }
}

运行后,直接报错:

Caused by: <openjpa-2.4.1-r422266:1730418 fatal user error> org.apache.openjpa.util.MetaDataException: The type "class com.pp.jpa.spring.openjpa.User" 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)
	at org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepository.java:650)
	at org.apache.openjpa.meta.MetaDataRepository.getMetaDataInternal(MetaDataRepository.java:418)
	at org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository.java:389)
	at org.apache.openjpa.persistence.meta.MetamodelImpl.<init>(MetamodelImpl.java:86)
	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.getMetamodel(EntityManagerFactoryImpl.java:348)
	at org.apache.openjpa.persistence.EntityManagerFactoryImpl.getMetamodel(EntityManagerFactoryImpl.java:59)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:407)
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:560)
	at com.sun.proxy.$Proxy15.getMetamodel(Unknown Source)
	at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.getMetamodels(JpaMetamodelMappingContextFactoryBean.java:90)
	at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:56)
	at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:26)
	at org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:134)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1641)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
	... 11 more
网上说,openjpa要配置运行时增强,但是,我直接用openjpa的时候,为啥不报错。

求解答







如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野心澎湃 2021-12-03 06:11:34

引用来自“jspp”的评论

The ChangeRoutineConsumedPK class was not added to the persistence.xml and i had turned off automatic class scanning.

Adding the class to the persistence.xml fixed the problem

<persistence-unit ...> ... <class>au.com.combined.domain.changeroutine.ChangeRoutineConsumedPK</class> ... </persistence-unit>

要么开启自动扫描,要么扫描范围主动加入这个 bean

参阅:

http://stackoverflow.com/questions/19042416/the-type-class-has-not-been-enhanced-jpa-exception

后知后觉 2021-12-02 18:35:49

The ChangeRoutineConsumedPK class was not added to the persistence.xml and i had turned off automatic class scanning.

Adding the class to the persistence.xml fixed the problem

<persistence-unit ...> ... <class>au.com.combined.domain.changeroutine.ChangeRoutineConsumedPK</class> ... </persistence-unit>

要么开启自动扫描,要么扫描范围主动加入这个 bean

参阅:

http://stackoverflow.com/questions/19042416/the-type-class-has-not-been-enhanced-jpa-exception

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文