0

I'm launching tomcat server with a jpa project.I got the error.

 java.lang.ClassNotFoundException: javax.persistence.criteria.Selection

this is my pom.xml

    <dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>javax.el-api</artifactId>
        <version>3.0.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet.jsp.jstl</groupId>
        <artifactId>javax.servlet.jsp.jstl-api</artifactId>
        <version>1.2.1</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>javax.servlet.jsp.jstl</artifactId>
        <version>1.2.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
            </exclusion>
            <exclusion>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
            </exclusion>
            <exclusion>
                <groupId>javax.servlet.jsp.jstl</groupId>
                <artifactId>jstl-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>javax.websocket</groupId>
        <artifactId>javax.websocket-api</artifactId>
        <version>1.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.3.2</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j.version}</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j.version}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-jcl</artifactId>
        <version>${log4j.version}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>${log4j.version}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-taglib</artifactId>
        <version>${log4j.version}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.2</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.jboss.logging</groupId>
        <artifactId>jboss-logging</artifactId>
        <version>3.2.0.Beta1</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.6.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.6.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>

    <!-- Javaee API -->
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>6.0</version>
    </dependency>
        <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>javax.transaction-api</artifactId>
        <version>1.2</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.6.Final</version>
        <scope>runtime</scope>
        <exclusions>
            <exclusion>
                <groupId>org.jboss.spec.javax.transaction</groupId>
                <artifactId>jboss-transaction-api_1.2_spec</artifactId>
            </exclusion>
            <exclusion>
                <groupId>xml-apis</groupId>
                <artifactId>xml-apis</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.jboss.logging</groupId>
                <artifactId>jboss-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <sourceDirectory>source/production/java</sourceDirectory>
    <resources>
        <resource>
            <directory>source/production/resources</directory>
        </resource>
    </resources>

    <testSourceDirectory>source/test/java</testSourceDirectory>
    <testResources>
        <testResource>
            <directory>source/test/resources</directory>
        </testResource>
    </testResources>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <warSourceDirectory>web</warSourceDirectory>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

I refer to this answer java.lang.ClassNotFoundException: javax.persistence.Persistence cannot be found with JPA,but it can't solve my problem.

And this is my project Chapter 20/Eclipse/Entity-Mappings/ which comes from ProfessionalJavaforWebApplications

K.Nicholas
  • 10,956
  • 4
  • 46
  • 66
MarsForever
  • 21
  • 1
  • 6
  • Welcome to Stackoverflow. Please see [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – K.Nicholas Oct 30 '18 at 18:58
  • I add hibernate-jpa-2.1-api-1.0.0.Final.jar to Referenced Libraries. – MarsForever Oct 31 '18 at 14:20
  • Builds find for me as is. Again, don't just post examples from books here that you cannot get working. Trying working through all the chapters first and make sure you can build something before trying to build chapter 20. – K.Nicholas Oct 31 '18 at 16:22

2 Answers2

0

javax.persistence.criteria.Selection is in both hibernate-jpa-2.1-api and javaee-api. I don't think you should have hibernate-jpa-2.1-api in your dependencies.

Bernie
  • 2,253
  • 1
  • 16
  • 18
  • I add `hibernate-jpa-2.1-api-1.0.0.Final.jar` to Referenced Libraries.And `hibernate-jpa-2.1-api-1.0.0.Final.jar` also added to target/projectname/WEB-INF/lib – MarsForever Oct 31 '18 at 14:23
  • Yes, I can see that you have `hibernate-jpa-2.1-api` as a dependency. I don't think that you should. The GitHub project says that it is "intended for transitory use in Hibernate JPA development". The classes in that library are also in `javaee-api` so you shouldn't need that Hibernate library. – Bernie Oct 31 '18 at 21:00
  • In your GitHub project, you've excluded `hibernate-jpa-2.1-api`, but you also don't have `javaee-api`. If you're not including `javaee-api` as a dependency, then don't exclude `hibernate-jpa-2.1-api`. You'll need one or the other. – Bernie Oct 31 '18 at 21:14
  • Thank you for looking my GitHub project carefully,I add javaee-api jar dependency to `pom.xml`.And I recognized the core problem is not I don't have `hibernate-jpa-2.1-api` and `javaee-api`.Because I added them to my Referenced Libraries.But my project can't recognized it in pom's libraries.So I executed those steps.1.eclipse:clean 2.eclipse:eclipse 3.Maven->Update project 4.Run as Maven clean 5.Run as Maven install.And then it worked. – MarsForever Nov 02 '18 at 01:05
0

It looked like my project didn't recognized pom's libraries.So I added 'javaee-api' to pom.xml(I added it to libraries before but it didn't work in pom). And then executed those steps.

  1. eclipse:clean
  2. eclipse:eclipse
  3. Maven->Update project
  4. Run as Maven clean
  5. Run as Maven install.And then it worked
MarsForever
  • 21
  • 1
  • 6