0

我没有太多使用 Maven,但我知道它首先检查本地存储库以获取所需的 jar 文件,如果在那里找不到,它会从提到的存储库下载它。在我的程序中,jar 文件位于本地存储库中的所需位置。pom.xml 文件也运行良好,但程序仍然无法在任何文件中找到导入。

程序中的测试类:

import javax.persistence.Cacheable;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class TestAnnotation {
    public static void main(String args[]){
        EntityManagerFactory factory=Persistence.createEntityManagerFactory("annotation");
        EntityManager manager=factory.createEntityManager();
        EntityTransaction transaction=manager.getTransaction();
        transaction.begin();    
        manager.persist(new Message("My Entity Test One More Example New"));
        transaction.commit();
        System.out.println("First time calling Message Object");
        getMessage(manager,23);
        System.out.println("Second time calling Message Object");
        getMessage(manager,23);

        factory.close();
    }


    public static void getMessage(EntityManager manager,long id){
        EntityTransaction transaction1=manager.getTransaction();
        transaction1.begin();   
        Query q=manager.createQuery("from Message m where m.id="+id);
        Message m=(Message)q.getSingleResult();
        System.out.println(m.getMessage_text());
        transaction1.commit();
    }
}

程序中的简单POJO:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name="MESSAGES")
@Cache(region = "messages", usage = CacheConcurrencyStrategy.READ_WRITE)

public class Message {

    Message(){

    }
    Message(String message){
        message_text=message;
    }
    @Id @GeneratedValue
    @Column(name="MESSAGE_ID")
    public Long id;
    @Column(name="MESSAGE_TEXT")
    public String message_text;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getMessage_text() {
        return message_text;
    }
    public void setMessage_text(String message_text) {
        this.message_text = message_text;
    }
}

最后是有问题的 pom.xml 文件:

<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>HibernateWithAnnotation</groupId>
    <artifactId>HibernateWithAnnotation</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>HibernateWithAnnotation</name>
    <repositories>
        <repository>
            <id>codelds</id>
            <url>https://code.lds.org/nexus/content/groups/main-repo</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.1-Final</version>

        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>3.2.0.Final</version>
        </dependency>


        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.5.1-Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.5.1-Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>


        <!-- logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.6.1</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.1</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>


        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-core</artifactId>
            <version>2.4.5</version>
        </dependency>


    </dependencies>
</project>

pom.xml 不包含任何错误。它运行良好的目标 - clean package。但是我的 java 文件中使用的导入仍然没有解决。我一定在某个地方发生了一些愚蠢的事情。请向我指出。

4

4 回答 4

1

我假设你正在使用eclipse ..

尝试执行mvn eclipse:eclipse. 这将设置所有必需的类路径条目。

于 2013-05-31T11:01:25.737 回答
1

Go to the directory containing pom.xml. open command prompt and run the following command :

mvn install

Then go back to eclipse refresh your project. All dependencies would be configured

于 2013-05-31T11:10:15.463 回答
0

有两种不同的东西——用 IDE 开发和用 maven 构建一切。

Maven 是独立的东西,它不知道您使用的任何 IDE。

您必须配置自己喜欢的 IDE-maven 插件,或者手动将 jars 添加到类路径中

于 2013-05-31T11:01:52.487 回答
0

Maven 依赖项仅在运行时使用。如果您使用任何类型的 IDE 进行编码,则必须将所需的 jar 添加到类路径中,以便在您键入时,将为您的关键字显示建议和参考注释。

于 2013-06-29T10:17:20.193 回答