简体   繁体   中英

POM.xml runs fine but still the imports of the java files are not resolved

I have not used Maven much but I know it first checks into the local repository for the desired jar files and if not found there it downloads it from a mentioned repository. In my program, the jar files are present at the desired location inside the local repository. pom.xml file also runs fine but still the program is not able to find the imports in any of the files.

Test class in the Program:

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();
    }
}

Simple POJO in the program:

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;
    }
}

And finally the problematic pom.xml file:

<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 doesnt contain any error as such. It runs fine for the goals - clean package . But still the imports used in my java files are not resolved. There must be something silly happening by me somewhere. Please point it out to me.

I am assuming you are using eclipse..

Try executing mvn eclipse:eclipse . This sets up all the required classpath entries.

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

There are 2 different things - developing with IDE and building everything with maven.

Maven is standalone thing and it does not know anything of IDEs you use.

You have to either configure you favorite IDE-maven plugin, or just add the jars to the classpath by hand

Maven dependencies are used only during run time. If you are using any kind of IDE to code, you have to add the required jars to class path so that when ever you type, the suggestions and reference comments will be shown for your keyword.

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