OpenJPA和Spring-boot配置 [英] OpenJPA and Spring-boot configuration

查看:150
本文介绍了OpenJPA和Spring-boot配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使用spring-boot应用程序的基本配置,并且将OpenJPA作为jpa实现.基本上,我总是以:

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

由以下原因引起:org.apache.openjpa.util.MetaDataException:类型"com.openjpa.example.Customer类"没有得到增强.

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

我的配置如下:

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

和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 ?

推荐答案

此处是pom条目,用于在构建时增强实体类.它为我工作. 还请注意,在构建时需要META-INF/persistence.xml来增强类,而在运行时则不需要.因此,您可以在创建jar时排除persistence.xml.

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>

这篇关于OpenJPA和Spring-boot配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆