2

I am trying to add javax.persistence.Id as an annotation to a filed and generate as Java objects through Maven JAXB plugin. However I run into class not found exception for javax.persistence.id I did make sure that the javax.persistence is included in the maven dependency and I see maven pulling it as dependency but when I run through jaxb plugin it won't work.

  1. Here is my XML <xsd:complexType name="MyTable"> <xsd:sequence> <xsd:element name="id" type="xsd:int"/> </xsd:sequence> </xsd:complexType>
  2. Here is my binding.xjb file
<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. Here is my relevant 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-basics 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>
    

However when I run Maven-->Generate-Sources I get this error

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

If I simply add @Id annotation to any java class in the project then I can add and I see javax.persistence.Id getting imported with no problem. What is going wrong when I use maven & binding.xjb? Am I not defining the annotation properly? Many thanks!

lazyguy
  • 963
  • 1
  • 13
  • 33

1 Answers1

10

I guess it's also the same problem that I had: you added javax.persistence as a Maven dependency, but not as a dependency to your JAXB plugin:

Add something like this (if you're using 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>

I had a similar issue and was able to solve it with that. In the following example please note that I'm putting the annotations into the XSD and not into the XJB file but the Maven configuration should be similar.

Here's my 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>

And here are the relevant sections from my 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>

I had the same exception, this fixed it. Just be aware that my example puts everything into the XSD and not into the XJB... but the dependency-thing mentioned above should help you as well.

Rias A. Sherzad
  • 748
  • 7
  • 13