简体   繁体   中英

javax.persistence.EntityManager missing a method

I have recently tired to fix the problem with maven dependencies in my java web project and I deleted all things from the repository folder, after that I did "maven update project" in eclipse and the problem was fixed, however other error appeared. The method I was using createNamedQuery(String, Class<T>) no longer exists in the javax.persistence.EntityManager interface. I did some research and I suspect that maven has changed the JPA implementation version to JPA 1.0 which is very annoying and also I don't really know how to fix this. I didn't change anything in pom.xml , the only change that I noticed is that javax.persistence package now comes from ejb3 jar from jboss-as-connector maven artifact. How can I fix this?

SORRY: I just found out that the ejb3-persistence comes from hibernate package.

EDIT:

pom.xml file:

    ...
<properties>
    <jboss.provided.jar>compile</jboss.provided.jar>
</properties>

<!-- JBoss repository for JMS stuff -->
<repositories>
    <repository>
        <id>JBoss repository</id>
        <url>http://repository.jboss.org/nexus/content/groups/public/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
    </dependency>
    <!-- Provides better parameterized tests than those provided by junit -->
    <dependency>
        <groupId>pl.pragmatists</groupId>
        <artifactId>JUnitParams</artifactId>
        <version>1.0.2</version>
        <scope>test</scope>
    </dependency>
    <!-- Required for SpringJUnit4ClassRunner in IntegrationTests -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${org.springframework.version}</version>
        <scope>test</scope>
    </dependency>
    <!-- Required for SpringockitoContextLoader in IntegrationTests -->
    <dependency>
        <groupId>org.kubek2k</groupId>
        <artifactId>springockito-annotations</artifactId>
        <version>1.0.8</version>
        <scope>test</scope>
    </dependency>
    <!-- Required for created the hsql database in IntegrationTests -->
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>2.3.1</version>
        <scope>test</scope>
    </dependency>
    <!-- Required for Selenium WebDriver automated tests -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.33.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>xml-apis</groupId>
        <artifactId>xml-apis</artifactId>
        <version>1.4.01</version>
        <scope>test</scope>
    </dependency>

    THIS IS PROBABLY THE ROOT OF ALL EVIL
    <!-- JBoss Seam - stuff for JMS Queue on JBoss -->
    <dependency>
        <groupId>org.jboss.jbossas</groupId>
        <artifactId>jboss-as-connector</artifactId>
        <version>6.1.0.Final</version>
    </dependency>


    <!-- Required when running the integration tests -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

    <!-- aspectjrt and aspectjweaver both required for @Autowired annotation 
        to work -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.7.3</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.7.3</version>
    </dependency>

    <!-- Required for spring bean proxying to work in integration tests -->
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib-nodep</artifactId>
        <version>2.1_3</version>
    </dependency>

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <!-- Required for @RequestMapping annotation -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${org.springframework.version}</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <!-- Use the same hibernate and Mysql versions as those on the JBoss server -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${org.hibernate.version}</version>
        <scope>${jboss.provided.jar}</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.0.1.Final</version>
        <scope>${jboss.provided.jar}</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.22</version>
        <scope>${jboss.provided.jar}</scope>
    </dependency>

    <!-- Use slf4j and log4j for logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.5</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
        <scope>${jboss.provided.jar}</scope>
    </dependency>

    <!-- Required for communication with exchange -->
    <dependency>
        <groupId>com.microsoft.ewsjavaapi</groupId>
        <artifactId>exchange-ws-api</artifactId>
        <version>1.1.5.2</version>
    </dependency>
    <dependency>
        <groupId>commons-httpclient</groupId>
        <artifactId>commons-httpclient</artifactId>
        <version>3.1</version>
    </dependency>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>jcifs</groupId>
        <artifactId>jcifs</artifactId>
        <version>1.3.17</version>
    </dependency>
    <!-- end of dependencies for exchange -->

    <!-- Required for FileUtils -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>

    <!-- Required for StringUtils -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.1</version>
    </dependency>
</dependencies>
<build>
    <finalName>cat-webapp</finalName>

...
  1. To find out which library is this class comes from just call Thread.currentThread().getContextClassLoader().getResource("javax/persistence/EntityManager.class")

  2. To see which dependency is this goes from invoke mvn dependency:tree

Replace this dependency:

<dependency>
    <groupId>org.jboss.jbossas</groupId>
    <artifactId>jboss-as-connector</artifactId>
    <version>6.1.0.Final</version>
</dependency>

with

 <dependency>
    <groupId>org.jboss.as</groupId>
    <artifactId>jboss-as-connector</artifactId>
    <version>7.1.1.Final</version>
 </dependency>

You will get EntityManager interface from hibernate-jpa-2.0-api with the createNamedQuery method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM