OpenJPA & Spring Boot 2 & Gradle

Piotr Sagalara :

I'm trying to run OpenJPA with newest Spring Boot 2 and Gradle.

The problem is that Spring 5 does not support OpenJPA anymore.

When I run the application I see an error:

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".

There was some plugin for older Gradle which was doing the entity enhancement with the use of openjpa library, but it does not work with the newer.

I have used an adapter and dialect like in this repo: https://github.com/apache/syncope/tree/master/core/persistence-jpa/src/main/java/org/springframework/orm/jpa/vendor

I have the persistence.xml file in resources/jpa/persistence.xml I have also tried to move it to 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>

My configuration for 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;
  }
}

Test Entity:

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;
  }
}

And finally the main Application class:

@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);
  }
}
Piotr Sagalara :

The best solution for me was to add a plugin from this page:

https://github.com/radcortez/openjpa-gradle-plugin

Another solution is to set a javaagent in VM options: -javaagent:/path/openjpa-all-3.1.0.jar

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=80955&siteId=1
Recommended