Spring Data JPA(openjpa) 报错:has not been enhanced
直接使用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>
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
引用来自“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
要么开启自动扫描,要么扫描范围主动加入这个 bean
参阅:
http://stackoverflow.com/questions/19042416/the-type-class-has-not-been-enhanced-jpa-exception
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
要么开启自动扫描,要么扫描范围主动加入这个 bean
参阅:
http://stackoverflow.com/questions/19042416/the-type-class-has-not-been-enhanced-jpa-exception