2

我正在尝试将javax.persistence.Id作为注释添加到文件中,并通过 Maven JAXB 插件生成为 Java 对象。但是,我遇到了 javax.persistence.id 的 class not found 异常,我确实确保 javax.persistence 包含在 maven 依赖项中,并且我看到 maven 将其作为依赖项拉,但是当我通过 jaxb 插件运行时它不会工作。

  1. 这是我的 XML<xsd:complexType name="MyTable"> <xsd:sequence> <xsd:element name="id" type="xsd:int"/> </xsd:sequence> </xsd:complexType>
  2. 这是我的 binding.xjb 文件
<jaxb:bindings version="2.1"
   xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
   xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
   xmlns:annox="http://annox.dev.java.net" jaxb:extensionBindingPrefixes="annox">

<jaxb:bindings schemaLocation="mytable.xsd">

  <jaxb:bindings node="xs:complexType[@name='MyTable']/xs:sequence/xs:element[@name='id']">
       <annox:annotate target="field">
             <annox:annotate annox:class="javax.persistence.Id"/>
       </annox:annotate>
  </jaxb:bindings> 

</jaxb:bindings>

  1. 这是我的相关 Pom.xml

    <dependencies>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.9</version>
        </dependency>
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics</artifactId>
            <version>0.6.4</version>
        </dependency>
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics-annotate</artifactId>
            <version>0.6.4</version>
        </dependency>
        <dependency>
         <groupId>javax.persistence</groupId>
         <artifactId>persistence-api</artifactId>
         <version>1.0.2</version>
         <scope>provided</scope>
    </dependency>
     </dependencies>
    
     <build>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.8.3</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <configuration>
                            <forceRegenerate>true</forceRegenerate>
                            <schemaDirectory>myschema</schemaDirectory>
                            <bindingIncludes>
                                <include>binding.xjb</include>
                            </bindingIncludes>
                            <extension>true</extension>
                           <args>
                            <arg>-Xvalue-constructor</arg>
                            <arg>-XtoString</arg>
                            <arg>-Xequals</arg>
                            <arg>-XhashCode</arg>
                            <arg>-Xcopyable</arg>
                            <arg>-Xannotate</arg>
                        </args>
                            <plugins>
              <plugin>                         <groupId>org.jvnet.jaxb2_commons</groupId>
    

    jaxb2-基础 0.6.4

    org.jvnet.jaxb2_commons jaxb2-basics-annotate 0.6.4 org.jvnet.jaxb2_commons jaxb2-value-constructor 3.0

    </plugins>
                        </configuration>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.jvnet.jaxb2.maven2</groupId>
                                        <artifactId>maven-jaxb2-plugin</artifactId>
                                        <versionRange>[0.7.4,)</versionRange>
                                        <goals>
                                            <goal>generate</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    

但是,当我运行Maven-->Generate-Sources时出现此错误

Caused by: org.jvnet.annox.parser.AnnotationElementParseException: Could not parse the annotation element.
    at org.jvnet.annox.parser.XAnnotationParser.parse(XAnnotationParser.java:90)
    at org.jvnet.jaxb2_commons.plugin.annotate.AnnotatePlugin.annotate(AnnotatePlugin.java:387)
    ... 31 more
Caused by: org.jvnet.annox.annotation.AnnotationClassNotFoundException: Annotation class [javax.persistence.Id] could not be found.
    ... 33 more
Caused by: java.lang.ClassNotFoundException: javax.persistence.Id

如果我只是将 @Id 注释添加到项目中的任何 java 类,那么我可以添加并且我看到 javax.persistence.Id 被导入没有问题。当我使用 maven & binding.xjb 时出了什么问题?我没有正确定义注释吗?非常感谢!

4

1 回答 1

10

我想这也是我遇到的相同问题:您添加javax.persistence为 Maven 依赖项,但不是作为 JAXB 插件的依赖项:

添加类似这样的内容(如果您使用的是 Hibernate):

</project>
    ...
    <build>
      <plugins>
        <plugin>
          <groupId>org.jvnet.jaxb2.maven2</groupId>
          <artifactId>maven-jaxb2-plugin</artifactId>
          <version>${maven-jaxb2-plugin.version}</version>
          <executions>
             ...
          </executions>
          <configuration>
             ...
          </configuration>
          <dependencies>
            <!-- Hibernate Persistence Annotations -->
            <dependency>
              <groupId>org.hibernate.javax.persistence</groupId>
              <artifactId>hibernate-jpa-2.0-api</artifactId>
              <version>${hibernate-jpa-2.0-api.version}</version>
            </dependency>
          </dependencies>
        </plugin>
      </plugins>
    </build>
    ...
</project>

我有一个类似的问题,并且能够解决它。在下面的示例中,请注意我将注释放入 XSD 而不是 XJB 文件中,但 Maven 配置应该类似。

这是我的 XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1"
  elementFormDefault="qualified" targetNamespace="http://www.gl-group.com/ewelding/schemas"
  xmlns:ew="http://www.companyname.com/project/schemas"
  xmlns:xmime="http://www.w3.org/2005/05/xmlmime" jaxb:extensionBindingPrefixes="annox"
  xmlns:annox="http://annox.dev.java.net"
  xmlns:ja="http://annox.dev.java.net/javax.xml.bind.annotation"
  xmlns:jpa="http://annox.dev.java.net/javax.persistence"
  xmlns:solrj="http://annox.dev.java.net/org.apache.solr.client.solrj.beans"
  xmlns:jackson="http://annox.dev.java.net/com.fasterxml.jackson.annotation">

...
<xs:element name="Certificate">
  <xs:annotation>
    <xs:appinfo>
      <annox:annotate>
        <ja:XmlAccessorType value="NONE"/>
        <jpa:Entity name="Certificate"/>
        <solrj:Field value="testByOrderOf"/>
        <jackson:JsonFormat shape="STRING" pattern="yyyy-MM-dd'T'HH:mm:ss'Z'" timezone="GMT"/>
      </annox:annotate>
    </xs:appinfo>
  </xs:annotation>
  ...
  <xs:element ref="ew:dateOfBirth">
    <xs:annotation>
      <xs:appinfo>
        <annox:annotate target="field">
          <solrj:Field value="dateOfBirth"/>
          <jackson:JsonFormat shape="STRING" pattern="yyyy-MM-dd'T'HH:mm:ss'Z'" timezone="GMT"/>
        </annox:annotate>
      </xs:appinfo>
    </xs:annotation>
  </xs:element>
  ...
</xs:element>

以下是我的相关部分pom.xml

...
<properties>
  ...
  <maven-jaxb2-plugin.version>0.8.3</maven-jaxb2-plugin.version>
  <jaxb2-basics.version>0.6.4</jaxb2-basics.version>
  <jaxb2-value-constructor.version>3.0</jaxb2-value-constructor.version>
  <solr-solrj.version>4.3.0</solr-solrj.version>
  <jackson.version>2.2.2</jackson.version>
  <hibernate-jpa-2.0-api.version>1.0.1.Final</hibernate-jpa-2.0-api.version>
  ...
</properties>
...
<build>
  <plugins>
    ...
    <!-- Generate Java sources from XSD schema files -->
    <plugin>
      <groupId>org.jvnet.jaxb2.maven2</groupId>
      <artifactId>maven-jaxb2-plugin</artifactId>
      <version>${maven-jaxb2-plugin.version}</version>
      <executions>
        <execution>
          <goals>
            <goal>generate</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <extension>true</extension>
        <args>
          <arg>-Xannotate</arg>
          <arg>-Xvalue-constructor</arg>
          <arg>-Xinheritance</arg>
          <arg>-enableIntrospection</arg>
        </args>
        <!-- Include our schema -->
        <schemaDirectory>src/main/resources</schemaDirectory>
        <schemaIncludes>
          <include>certificate.xsd</include>
        </schemaIncludes>
        <bindingIncludes>
          <bindings>certificate.xjb</bindings>
        </bindingIncludes>
        <generateDirectory>src/main/java</generateDirectory>
        <generatePackage>com.company.project.generated</generatePackage>
        <plugins>
          <plugin>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics</artifactId>
            <version>${jaxb2-basics.version}</version>
          </plugin>
          <plugin>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics-annotate</artifactId>
            <version>${jaxb2-basics.version}</version>
          </plugin>
          <plugin>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-value-constructor</artifactId>
            <version>${jaxb2-value-constructor.version}</version>
          </plugin>
        </plugins>
      </configuration>
      <dependencies>
        <!-- SolrJ - only needed for the @Field annotation -->
        <dependency>
          <groupId>org.apache.solr</groupId>
          <artifactId>solr-solrj</artifactId>
          <version>${solr-solrj.version}</version>
        </dependency>

        <!-- Jackson2 Annotations -->
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-annotations</artifactId>
          <version>${jackson.version}</version>
        </dependency>

        <!-- Hibernate Persistence Annotations -->
        <dependency>
          <groupId>org.hibernate.javax.persistence</groupId>
          <artifactId>hibernate-jpa-2.0-api</artifactId>
          <version>${hibernate-jpa-2.0-api.version}</version>
        </dependency>
      </dependencies>
    </plugin>
    ...
  </plugins>
</build>

我有同样的例外,这解决了它。请注意,我的示例将所有内容都放入 XSD 而不是 XJB 中...但是上面提到的依赖项也应该对您有所帮助。

于 2013-06-17T11:16:55.050 回答