OpenJPA and Spring-boot configuration

喜你入骨 提交于 2019-12-12 12:16:01

问题


Im struggling with basic configuration of spring-boot app, with OpenJPA as jpa implementation. Basically I always end up with:

Caused by: org.apache.openjpa.util.MetaDataException: The type "class com.openjpa.example.Customer" has not been enhanced.

My configuration looks like below:

package com.openjpa.example;

    @SpringBootApplication
    public class Application extends JpaBaseConfiguration {
        protected Application(DataSource dataSource, JpaProperties properties,
                ObjectProvider<JtaTransactionManager> jtaTransactionManagerProvider) {
            super(dataSource, properties, jtaTransactionManagerProvider);
        }

        @Override
        protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
            OpenJpaVendorAdapter jpaVendorAdapter = new OpenJpaVendorAdapter();
            jpaVendorAdapter.setShowSql(true);
            return jpaVendorAdapter;
        }

        @Override
        protected Map<String, Object> getVendorProperties() {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("openjpa.Log", "DefaultLevel=TRACE, Tool=INFO, SQL=TRACE, Runtime=TRACE");
            map.put("openjpa.jdbc.MappingDefaults", "IndexLogicalForeignKeys=false,IndexDiscriminator=false");
            map.put("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)");
            map.put("openjpa.RuntimeUnenhancedClasses", "supported");
            map.put("openjpa.DynamicEnhancementAgent", "false");
            map.put("openjpa.weaving", "false");
            return map;
        }

        public static void main(String[] args) {
            CustomerRepository repository = SpringApplication.run(Application.class, args).getBean(CustomerRepository.class);
            repository.save(new Customer("Richard", "Feynman"));


            System.out.println(repository.findAll());
        }
    }

and pom.xml:

<profiles>
    <profile>
        <id>static-weaving</id>

            <build>
                <plugins>

                <plugin>
                <groupId>org.apache.openjpa</groupId>
                <artifactId>openjpa-maven-plugin</artifactId>
                <configuration>
                    <includes>**/openjpa/*.class</includes>
                    <addDefaultConstructor>true</addDefaultConstructor>
                    <enforcePropertyRestrictions>true</enforcePropertyRestrictions>

                </configuration>
                <executions>
                    <execution>
                        <id>enhancer</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>enhance</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.openjpa</groupId>
                        <artifactId>openjpa</artifactId>
                        <version>${openjpa.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
                </plugins>
            </build>

        </profile>

        <profile>

            <id>load-time-weaving</id>

            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>

            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <dependencies>
                            <dependency>
                                <groupId>org.springframework</groupId>
                                <artifactId>spring-instrument</artifactId>
                                <version>${spring.version}</version>
                            </dependency>
                        </dependencies>
                        <configuration>
                            <argLine>-javaagent:${settings.localRepository}/org/springframework/spring-instrument/${spring.version}/spring-instrument-${spring.version}.jar</argLine>
                        </configuration>
                    </plugin>
                    <plugin>

                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <dependencies>
                            <dependency>
                                <groupId>org.springframework</groupId>
                                <artifactId>spring-instrument</artifactId>
                                <version>${spring.version}</version>
                            </dependency>
                        </dependencies>
                        <configuration>
                            <agent>${settings.localRepository}/org/springframework/spring-instrument/${spring.version}/spring-instrument-${spring.version}.jar</agent>
                            <agent>${settings.localRepository}/org/apache/openjpa/openjpa/${openjpa.version}/openjpa-${openjpa.version}.jar</agent>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
    </profile>

</profiles>

Does anyone know what is wrong here ?


回答1:


Here is pom entry to enhance the entity classes at build time. It worked for me. Also please note that you need META-INF/persistence.xml to enhance the classes at build time and during runtime it is not required. So you can exclude persistence.xml while creating jar.

    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.openjpa</groupId>
                                    <artifactId>openjpa-maven-plugin</artifactId>
                                    <versionRange>2.2.0</versionRange>
                                    <goals>
                                        <goal>enhance</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <execute />
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                    <includes>**/domain/*.class</includes>
                    <addDefaultConstructor>true</addDefaultConstructor>
                    <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

    <plugins>
        .....
        <plugin>
            <groupId>org.apache.openjpa</groupId>
            <artifactId>openjpa-maven-plugin</artifactId>
            <version>2.2.0</version>
            <configuration>
                <includes>**/domain/*.class</includes>
                <addDefaultConstructor>true</addDefaultConstructor>
                <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
                <persistenceXmlFile>${project.basedir}/src/main/resources/META-INF/persistence.xml</persistenceXmlFile>
            </configuration>
            <executions>
                <execution>
                    <id>enhancer</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>enhance</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>

</build>


来源:https://stackoverflow.com/questions/40140463/openjpa-and-spring-boot-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!