gpt4 book ai didi

java - 如何在构建时使用 Maven 正确增强类?开放式JPA

转载 作者:行者123 更新时间:2023-12-03 23:00:02 24 4
gpt4 key购买 nike

我正在尝试增强(实体)类,因此我不会收到此异常:

Exception in thread "main" <openjpa-3.1.2-r66d2a72 nonfatal user error> org.apache.op
enjpa.persistence.ArgumentException: Attempt to cast instance "entities.EntityPerson@
6dba847b" to PersistenceCapable failed. Ensure that it has been enhanced.
FailedObject: entities.EntityPerson@6dba847b
at org.apache.openjpa.kernel.BrokerImpl.assertPersistenceCapable(BrokerImpl.java:486
3)
at org.apache.openjpa.kernel.BrokerImpl.persistInternal(BrokerImpl.java:2762)
at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2707)
at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2690)
at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2592)
at org.apache.openjpa.kernel.DelegatingBroker.persist(DelegatingBroker.java:1197)
at org.apache.openjpa.persistence.EntityManagerImpl.persist(EntityManagerImpl.java:8
37)
at openjpa.Test/main.Main.main(Main.java:23)
说我试图坚持的实例没有得到增强。
我关注了这个: https://openjpa.apache.org/enhancement-with-maven.html官方openjpa页面并添加 openjpa-maven-plugin到我的 pom.xml 部分:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>registry</groupId>
<artifactId>datalayer</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>

<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.sourceEncoding>UTF-8</project.reporting.sourceEncoding>
</properties>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa-maven-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<includes>datalayer/dto/*.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>
</plugin>
</plugins>
</pluginManagement>
</build>

<dependencies>
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.15.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbytools</artifactId>
<version>10.15.2.0</version>
</dependency>
</dependencies>
</project>
,但我仍然得到那个异常(exception)。我正在使用 Eclipse 并内置命令 Run As -> Maven install .我也尝试运行 mvn install从命令行。然后我只是尝试使用 EntityManager 来持久化一些实体.
运行 mvn install有和没有 openjpa-maven-plugin 不会改变任何东西 (使用相同的 .class 文件生成相同的 .jar,相同的 maven 输出到控制台)
正如答案所说,我需要运行 openjpa:enhance使用 Maven 构建时的目标。我实际上没有使用 openjpa-maven-plugin ,所以我解决了这个问题。 Maven 输出:
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< registry:datalayer >-------------------------
[INFO] Building datalayer 0.0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ datalayer ---
[INFO] Deleting C:\Users\wortigson\Desktop\ISMatrikaBcWorkspace\Openjpa.Test\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ datalayer ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ datalayer ---
[WARNING] Can't extract module name from geronimo-jms_1.1_spec-1.1.1.jar: geronimo.jm
s.1.1.spec: Invalid module name: '1' is not a Java identifier
[WARNING] Can't extract module name from geronimo-jta_1.1_spec-1.1.1.jar: geronimo.jt
a.1.1.spec: Invalid module name: '1' is not a Java identifier
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to C:\Users\wortigson\Desktop\ISMatrikaBcWorkspace\Op
enjpa.Test\target\classes
[INFO]
[INFO] --- openjpa-maven-plugin:3.1.2:enhance (default-cli) @ datalayer ---
130 DataLayer INFO [main] openjpa.Tool - Enhancer running on type "class entities
.EntityPerson".
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ datalayer ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ datalayer ---
[WARNING] Can't extract module name from geronimo-jms_1.1_spec-1.1.1.jar: geronimo.jm
s.1.1.spec: Invalid module name: '1' is not a Java identifier
[WARNING] Can't extract module name from geronimo-jta_1.1_spec-1.1.1.jar: geronimo.jt
a.1.1.spec: Invalid module name: '1' is not a Java identifier
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to C:\Users\wortigson\Desktop\ISMatrikaBcWorkspace\Op
enjpa.Test\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ datalay
er ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ datalayer
---
[WARNING] Can't extract module name from geronimo-jta_1.1_spec-1.1.1.jar: geronimo.jt
a.1.1.spec: Invalid module name: '1' is not a Java identifier
[WARNING] Can't extract module name from geronimo-jms_1.1_spec-1.1.1.jar: geronimo.jm
s.1.1.spec: Invalid module name: '1' is not a Java identifier
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ datalayer ---
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ datalayer ---
[INFO] Building jar: C:\Users\wortigson\Desktop\ISMatrikaBcWorkspace\Openjpa.Test\tar
get\datalayer-0.0.1.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ datalayer ---
[INFO] Installing C:\Users\wortigson\Desktop\ISMatrikaBcWorkspace\Openjpa.Test\target
\datalayer-0.0.1.jar to C:\Users\wortigson\.m2\repository\registry\datalayer\0.0.1\da
talayer-0.0.1.jar
[INFO] Installing C:\Users\wortigson\Desktop\ISMatrikaBcWorkspace\Openjpa.Test\pom.xm
l to C:\Users\wortigson\.m2\repository\registry\datalayer\0.0.1\datalayer-0.0.1.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
您可以看到我在 EntityPerson 类上运行了增强器,这是我测试过的简单 JPA 实体。但是我仍然得到那个异常(exception)。 我注意到 maven-compiler-plugin:3.8.1:compile运行两次。前后 openjpa-maven-plugin:3.1.2:enhance - 这可能是问题吗?
我认为这不是我的情况: How to enhance a class from a separate jar at build time with openJPA?
发现有解决方法 Attempt to cast instance to PersistenceCapable failed. Ensure that it's been enhanced通过在配置中设置一些属性作为接受的答案说,但我不想这样做,因为第一条评论说不推荐它并指向官方 openjpa 页面,在那里我发现了如何在 maven 构建期间增强类的示例时间,但正如我所写,它不起作用。
测试主要方法:
public static void main(String[] args)
{
HashMap<String, String> jpaProps = new HashMap<String, String>();
jpaProps.put("javax.persistence.jdbc.url", "jdbc:derby:testDB;create=true");
EntityManagerFactory factory = Persistence.createEntityManagerFactory("DataLayer", jpaProps);
EntityManager manager = factory.createEntityManager();
EntityPerson person = new EntityPerson();
person.setAge(25);
person.setName("Andrej Hruska");
manager.persist(person); // +
}
EntityPerson 类:
package entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class EntityPerson
{
@Id
@GeneratedValue
private long id;

@Column(nullable = false)
private String name;

private int age;

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

public int getAge()
{
return age;
}

public void setAge(int age)
{
this.age = age;
}
}
持久性.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence https://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="DataLayer"
transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>DataSource</non-jta-data-source>
<properties>
<property
name="javax.persistence.schema-generation.database.action"
value="create" />
<property name="javax.persistence.jdbc.driver"
value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:derby:db;create=true" />
</properties>
</persistence-unit>
</persistence>
项目结构:
Project structure
重现步骤:
  • Eclipse 中的 Crete Maven 项目
  • 复制我的 pom.xml(或将依赖项添加到 derby、openjpa 并设置您的 openjpa-maven-plugin、名称和路径)
  • 复制我的persistence.xml(或者用一些持久性单元创建你自己的)
  • 复制我的 EntityPerson 类(或创建简单的 JPA 实体以对其进行测试)
  • 运行 mvn clean compile openjpa:enhance install(重要部分是 openjpa:enhance)
  • 尝试像我在主要方法中所做的那样持久化实体
  • 最佳答案

    您需要告诉 Maven 运行增强目标。

    mvn clean compile openjpa:enhance
    你应该看到这个输出:
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ jpa-example ---
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 2 source files to /home/gordon/docs/programming/openjpa/jpa-example/target/classes
    [INFO]
    [INFO] --- openjpa-maven-plugin:3.1.2:enhance (default-cli) @ jpa-example ---
    [INFO]
    Maven 中有一个层次结构。顶部是由作为插件目标实现的阶段组成的生命周期。包装决定了一个阶段的目标。
    增强目标不是构建或编译的默认目标之一。
    在 eclipse 中,如果更改 pom.xml,则需要右键单击并选择 Maven->Update Project。您可以将 openjpa:enhance 放在 Maven Build 的 Eclipse 运行配置的目标字段中。
    Maven Build run configuration

    关于java - 如何在构建时使用 Maven 正确增强类?开放式JPA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66471893/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com