【问题标题】:import javax.persistence couldn't be resolved in my first Maven Hibernate Project在我的第一个 Maven Hibernate 项目中无法解决 import javax.persistence
【发布时间】:2015-07-24 17:10:48
【问题描述】:

在阅读了一个月的书籍和文章之后,我正在尝试创建我的第一个 Maven 项目 :) 但开始并不像我预期的那么顺利。我一直在摆弄我的 POM.xml 和各种依赖项以启用休眠注释,但似乎没有任何效果。我总是收到 javax.persistence.Entity 或任何其他注释在导入中未解决的错误。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>FirstHibernateExample</groupId>
      <artifactId>FirstHibernateExample</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>FirstHibernateExample</name>
      <description>This is my first project with Hibernate</description>
      <build>
        <sourceDirectory>src</sourceDirectory>
        <resources>
          <resource>
            <directory>src</directory>
            <excludes>
              <exclude>**/*.java</exclude>
            </excludes>
          </resource>
        </resources>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
        </plugins>
      </build>
       <repositories>
        <repository>
          <id>JBoss repository</id>
          <url>http://repository.jboss.com/maven2/</url>
        </repository>
        <repository>
            <id>Java.Net</id>
            <url>http://download.java.net/maven/2/</url>
        </repository>
      </repositories>
      <dependencies>
    <!-- Javaee API -->
<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
</dependency>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.31</version>
</dependency> 
<!-- Hibernate core -->
<dependency>
    <groupId>hibernate</groupId>
    <artifactId>hibernate3</artifactId>
    <version>3.2.3.GA</version>
</dependency>

<!-- Hibernate annotation -->
<dependency>
    <groupId>hibernate-annotations</groupId>
    <artifactId>hibernate-annotations</artifactId>
    <version>3.3.0.GA</version>
</dependency>

<dependency>
    <groupId>hibernate-commons-annotations</groupId>
    <artifactId>hibernate-commons-annotations</artifactId>
    <version>3.0.0.GA</version>
</dependency>

<!-- Hibernate library dependecy start -->
<dependency>
    <groupId>dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>1.6.1</version>
</dependency>

<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
</dependency>

<dependency>
    <groupId>commons-collections</groupId>
    <artifactId>commons-collections</artifactId>
    <version>3.2.1</version>
</dependency>

<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>2.2</version>
</dependency>
<!-- Hibernate library dependecy end -->

<dependency>
    <groupId>javax.transaction</groupId>
    <artifactId>jta</artifactId>
    <version>1.1</version>
</dependency>

谁能指出我错在哪里或者我应该怎么做才能让我的第一个项目运行?非常感谢提前:)

【问题讨论】:

  • 我已经从 mkyong.com 下载了一个现有的示例 maven hibernate 项目,甚至该项目在我的 eclipse 中也有同样的问题。我应该在类路径中包含我的 Maven 存储库吗?还是别的什么?
  • 感谢 ekansh :) 有时间我也会检查一下。

标签: java hibernate maven annotations


【解决方案1】:

最后,下面的命令解决了这个问题。 mvn eclipse:eclipse -DdownloadSource=true

我是从下面的链接中找到的 http://w3facility.org/question/javax-persistence-sources-where/

希望它对未来的人有所帮助:)

编辑:在另一个项目中,我只是右键单击错误导入语句并单击修复项目设置。它建议我在 M2_REPO 中将 buildpath 添加到 javaee-api6.0.jar 以解决该问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多