The import javax.persistence cannot be resolved after deleting Server Runtime Environment

16,862

Solution 1

To resolve: "The import javax.persistence cannot be resolved" you need to add the jar containing javax.persistence.Entity to your path. It is probably the hibernate-jpa-version-api jar.

To do this in Eclipse:

  • select the project from the Navigator or Package Explorer view
  • From the project menu at the top of the screen select properties
  • Click on Java Build Path and in the libraries tab add the peristence jar
  • Click OK

This should resolve the dependency issue you are having. If you want to add a server, go to file->new->other->server. Then choose the type of server you need.

Solution 2

I have downloaded the latest Hibernate version & I got the same issue after I added the below jars only, to my project.

Folder: hibernate-search-5.8.0.Final\dist\lib\required

Jars:

/antlr-2.7.7.jar
/classmate-1.3.0.jar
/dom4j-1.6.1.jar
/hibernate-commons-annotations-5.0.1.Final.jar
/hibernate-core-5.2.11.Final.jar
/javassist-3.20.0-GA.jar
/jboss-logging-3.3.0.Final.jar
/lucene-core-5.5.4.jar
/lucene-queryparser-5.5.4.jar
/xml-apis-1.3.03.jar

At this stage I'm facing the below issue.

The type javax.persistence.PersistenceException cannot be resolved. It is indirectly referenced from required .class files

So I have added one more dependent jar where the code is referencing internally and the issue vanished. Folder: hibernate-search-5.8.0.Final\dist\lib\provided

Jar Name: hibernate-jpa-2.1-api-1.0.0.Final.jar

Please post your comments, if you have any doubts and I'm glad to answer.

Share:
16,862

Related videos on Youtube

majd hwas
Author by

majd hwas

Updated on June 04, 2022

Comments

  • majd hwas
    majd hwas almost 2 years

    I had a working java project, I decided to remove all the Server Runtime Environments in my eclipse. since i am getting many errors like :

    The import javax.persistence cannot be resolved
    

    ,

    Entity cannot be resolved to a type
    

    ... can I restore the state of my project ?

    After thinking in logical reason , I think that the prbolem issued after that I had edited the PATH Variable in windows , can someone tell me what I shouldhave in this PATH variable to get the package javax.persistence working ?

  • majd hwas
    majd hwas about 8 years
    thanks Brod for this reply , so now I am sure that this is the problem that I have , I confirm that the project miss something that I had deleted from the Path variable , can you help me after this details ?
  • Brod
    Brod about 8 years
    Yes, follow the steps above. You simply need to know where the persistence jar is located. Have you tried the steps I listed above in the bullets? What isn't clear? Also checkout this other post: stackoverflow.com/questions/15598210/…
  • majd hwas
    majd hwas about 8 years
    Ok, I am not working with hibernate but with JPA , this issue started when I had deleted something from the path variable, can you guide me what i should have in this variable ?
  • Brod
    Brod about 8 years
    If you are not using hibernate then you may be looking for the persistence.jar. If you don't have persistence.jar or don't know where it is you can download it, then put it in a lib folder in your project and then follow the steps above. If you are using maven, just add it as a dependency. You can find the jar here: mvnrepository.com/artifact/javax.persistence/persistence-api‌​/…
  • Brod
    Brod about 8 years
    Another option, if you right click on your .classpath file and choose Compare With->Local History then look at the diffs from the different version. You should find the missing entry in your classpath this way.
  • majd hwas
    majd hwas about 8 years
    Ok , this seems to be good solution, but Where do i find this .classpath file with eclipse ?
  • Brod
    Brod about 8 years
    The classpath should be in the root of your project