我正在尝试将javax.persistence.Id作为注释添加到文件中,并通过 Maven JAXB 插件生成为 Java 对象。但是,我遇到了 javax.persistence.id 的 class not found 异常,我确实确保 javax.persistence 包含在 maven 依赖项中,并且我看到 maven 将其作为依赖项拉,但是当我通过 jaxb 插件运行时它不会工作。
- 这是我的 XML
<xsd:complexType name="MyTable"> <xsd:sequence> <xsd:element name="id" type="xsd:int"/> </xsd:sequence> </xsd:complexType>
- 这是我的 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>
这是我的相关 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 时出了什么问题?我没有正确定义注释吗?非常感谢!