我正在尝试使用最新的Spring Boot 2和Gradle来运行OpenJPA。
问题在于,Spring 5不再支持OpenJPA。
当我运行应用程序时,会看到一个错误:
Caused by: org.apache.openjpa.persistence.ArgumentException: This configuration disallows runtime optimization, but the following listed types were not enhanced at build time or at class load time with a javaagent: "
aero.onair.accground.aft.TestEntity".
以前的Gradle有一个插件,使用openjpa库进行实体增强,但它在新版本中不再适用。
我使用了像这个仓库中的适配器和方言: https://github.com/apache/syncope/tree/master/core/persistence-jpa/src/main/java/org/springframework/orm/jpa/vendor
我在resources/jpa/persistence.xml中有persistence.xml文件。 我还试图将它移动到resources/META-INF/
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="2.0">
<persistence-unit name="aftDbUnitName">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>aero.onair.accground.aft.TestEntity</class>
</persistence-unit>
</persistence>
我的 OpenJPA 配置:
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.openjpa.persistence.PersistenceProviderImpl;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.transaction.jta.JtaTransactionManager;
@Configuration
public class OpenJpaConfig extends JpaBaseConfiguration {
protected OpenJpaConfig(DataSource dataSource,
JpaProperties properties,
ObjectProvider<JtaTransactionManager> jtaTransactionManager,
ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
super(dataSource, properties, jtaTransactionManager, transactionManagerCustomizers);
}
@Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
OpenJpaVendorAdapter jpaVendorAdapter = new OpenJpaVendorAdapter();
jpaVendorAdapter.setShowSql(true);
return jpaVendorAdapter;
}
@Override
protected Map<String, Object> getVendorProperties() {
return new HashMap<>(0);
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setDataSource(getDataSource());
factory.setPersistenceProviderClass(PersistenceProviderImpl.class);
factory.setJpaVendorAdapter(new OpenJpaVendorAdapter());
factory.setPersistenceXmlLocation("jpa/persistence.xml");
return factory;
}
}
测试实体:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class TestEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
最后,主要的应用程序类:
@SpringBootApplication(exclude = {XADataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@Slf4j
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Autowired
TestEntitiyRepository testEntitiyRepository;
@PostConstruct
public void test() {
long count = testEntitiyRepository.count();
log.info("Count = {}", count);
TestEntity entitiy = new TestEntity();
entitiy.setName("testtttt");
testEntitiyRepository.save(entitiy);
count = testEntitiyRepository.count();
log.info("Count after save = {}", count);
}
}