1

After I tried unsuccessfully to add ehcache dependencies manually in a hibernate project using IntelliJ Idea , I decided to use maven and I added maven framework support to the project.Now I have an existing pom.xml file and I want to add the pom (with its dependencies) from this location (http://repo1.maven.org/maven2/org/hibernate/hibernate-ehcache/4.1.9.Final/). How do I do that? Can I have more than one pom.xml file in a project? Thank you.

Here is the current pom.xml file I am using :

<?xml version="1.0" encoding="UTF-8"?>
<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>groupId</groupId>
    <artifactId>HibernateProject1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.1.9.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>4.1.9.Final</version>
    </dependency>
    </dependencies>


</project>

Now I am getting the following error :

Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found

skiabox
  • 3,449
  • 12
  • 63
  • 95

2 Answers2

4

You can open only one IDEA project per IDEA window. But a Maven project can be made of multiple modules, each one having its own pom.xml. You can also import multiple Maven projects in the same IDEA project.

If you have an existing Maven project, just open the root pom.xml using File > Open and IDEA will ask you if it should be opened as a Maven project (providing you have enabled the Maven plugin in IDEA). When you make changes to a pom.xml, IDEA will suggest you to reimport the project, thus adding/removing libraries to synchronize the IDEA project with the Maven project.

To add a dependency to hibernate, add this code:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.1.9.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-ehcache</artifactId>
    <version>4.1.9.Final</version>
</dependency>
Bastien Jansen
  • 8,756
  • 2
  • 35
  • 53
  • Adding maven framework support after project creation, seems to create more problems.Do you believe that I should create a maven project from the first step of project creation wizard in IntelliJ Idea?What is your experience? – skiabox Mar 13 '13 at 12:30
  • I've added the two dependencies you suggest (you can check the edited original post) but I am getting an error. – skiabox Mar 13 '13 at 12:39
  • @ben75 I never said you can't have only one pom.xml per window, I said that you can have one maven project containing multiple pom.xml (modules). – Bastien Jansen Mar 13 '13 at 12:44
  • 1
    yes, but what I'm saying is that you an have more than one maven project per window: you can have 2 (or more) totally independant maven project (single or multi module) in one IDEA window (see my answer) – ben75 Mar 13 '13 at 12:48
  • @skiabox IDEA and maven works very well together. The only step I always do when I start to work with a new maven project under IDEA is to open it by choosing it's pom.xml file (instead of adding maven support later) – ben75 Mar 13 '13 at 13:02
  • I created a maven project from scratch and then I added hibernate support.The interesting thing is that I am getting the same exact error : Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found – skiabox Mar 13 '13 at 13:10
  • One problem that I have is that I have no resources folder! – skiabox Mar 13 '13 at 13:14
  • I've created a resources folder manually and I putted hibernate.cfg.xml in there.Now I am getting the following error : Exception in thread "main" org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.ehcache.EhCacheRegionFactory] – skiabox Mar 13 '13 at 13:23
  • @ben75 I clarified my answer to remove the confusion between multiple Maven/IDEA projects :). skiabox could you post the full stacktrace in your original question please? Also update the posted pom.xml if it has changed. – Bastien Jansen Mar 13 '13 at 13:33
  • This site helped me put all things in order and solved all my problems : http://mvnrepository.com/ – skiabox Mar 13 '13 at 13:36
1

You can have multiple maven project in one IDEA window.

You can always open a project from it's pom.xml file (if it's a multi-module project: all sub modules will be included automatically in your IDEA window).

When you have a maven project (single module or multi-module) open, you can go to the tab "Maven Project" (usually on the right side of the window). In this tab you can click on the "Green +" button : "Add maven project" and then browse to another pom.xml and select it: all the modules define in this other pom.xml will be added to your current IDEA window.

ben75
  • 29,217
  • 10
  • 88
  • 134