4

I'm trying to use Hibernate for the first time, and early on in the getting started guide, it makes reference to Maven. If I'm not mistaken, Maven appears to be a build tool. However, I've been using Eclipse to build my project up to this point.

Is there a way for me to use Hibernate without needing Maven? Can I just do what I need through Eclipse? Does anyone have a link to a resource that can show me how to do this?

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Matt Huggins
  • 81,398
  • 36
  • 149
  • 218

5 Answers5

8

No, of course not. I never use Maven - ever, for anything. Keep using Eclipse to build your projects.

I find that it's hard enough to learn one new thing at a time. If you're just learning Hibernate, why complicate your life with Maven?

Update: It's been six years since I wrote this answer. You still don't need Maven to use Hibernate, but I've changed my mind about which one I'd recommend.

I've learned that Maven is the best way to manage dependencies and project lifecycle. Hibernate? Still no reason for it. Keep it simple and stay away from ORM until it's essential.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • I have been trying to start learning Hibernate for 2 days. I assumed that it would be easier to follow the popular way 'building and running with maven'. It was not actually. I have no idea what is going on and try to understand what is missing or wrong. So far, I still do not know the basics of Hibernate and I am still looking for a way to build and run it. Kinda annoying... – Eray Tuncer Jun 06 '15 at 12:32
  • Why add two things you're ignorant of? Break the problem down. Why do you think you need Hibernate? Maybe that's the real problem. What benefit is it going to provide you? I see none. – duffymo Jun 06 '15 at 14:06
  • IMO the manual process of handling dependencies is alot harder then just letting maven do all the work for you. Eclipse has already maven preinstalled so basicly all you have to do is copy the the dependecy from hybernate into your pom.xml and klick update. – nanobot Apr 28 '19 at 14:22
  • Read my entire answer: I also recommend Maven. – duffymo Apr 28 '19 at 14:23
  • Maybe I missunderstood you. IMO you should allways use a build tool like maven or gradle if you have any external libs even if its just hybernate. Not only because its faster but it also has stuff like automated tests. Also if you want to share your code you would need to upload also the lib folder. If you want to update something it will become a mess. Reasons are endless. Sure if you write just HalloWorld you dont need it but for everything else at least I dont see a downside. – nanobot Apr 28 '19 at 14:28
  • We agree. Please note that the original question and answer are nine years old. Our discussion isn’t adding anything new. You should answer new questions if you want to contribute to this site. – duffymo Apr 28 '19 at 14:30
4

You do not need maven to use Hibernate. Maven offers the ease of having a single dependency to include all the jars necessary. Without maven you will have to download and import each jar that hibernate relies on. This would be a manual process.

Hibernate does not rely on maven as it does say javax.sql.

John Vint
  • 39,695
  • 7
  • 78
  • 108
  • 2
    Also you *can* use maven inside eclipse. Checkout the m2eclipse plugin if you are interested. – Chris Nava Aug 03 '10 at 04:15
  • @Chris Nava, youre right and it works pretty good too. Also look at the maven-eclipse-plugin which builds your .project nicely. – John Vint Aug 03 '10 at 14:38
2

You don't need Maven to use Hibernate. Without Maven, you'll just have to download and add the jar dependencies to the project manually, which isn't hard. And you can use whichever build process you use.

samitgaur
  • 5,601
  • 2
  • 29
  • 33
2

Well, I am late in this answer. But nevertheless it will help the future readers. Here I am adding how to configure a project without Maven.

Configuration

  1. Download Hibernate 5 from here.
  2. Extract the Required jars from the folder inside and add it to the build path in following hibernate-release-5.0.7.Final.zip\hibernate-release-5.0.7.Final\lib\required.
  3. Make sure the following jars are added to the classpath of the project:

    antlr-2.7.7.jar
    commons-dbcp2-2.1.1.jar
    dom4j-1.6.1.jar
    geronimo-jta_1.1_spec-1.1.1.jar
    hibernate-commons-annotations-5.0.1.Final.jar 
    hibernate-core-5.0.7.Final.jar
    hibernate-jpa-2.1-api-1.0.0.Final.jar
    jandex-2.0.0.Final.jar
    javassist-3.18.1-GA.jar
    javax.servlet.jsp.jstl-api-1.2.1-sources.jar
    jboss-logging-3.3.0.Final.jar
    sqljdbc4-3.0.jar //or whatever database you use
    hibernate-entitymanager-5.0.7.Final
    

Code:

Employee.java

import java.io.Serializable;

public class Employee implements Serializable{

   private static final long serialVersionUID = 1L;
   private int id;  
   private String firstName;
   private String lastName;
   private int salary;
   //setters and getters  
 }

HibernateUtil.java :

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

  private static final SessionFactory sessionFactory = buildSessionFactory();

  private static SessionFactory buildSessionFactory() {
    try {
        // Create the SessionFactory from hibernate.cfg.xml
        return new Configuration().configure().buildSessionFactory();
    }
    catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
     }
 }

 public static SessionFactory getSessionFactory() {
     return sessionFactory;
 }

 public static void shutdown() {
    // Close caches and connection pools
    getSessionFactory().close();
  }

}

Accessing Hibernate :

 public void hibernateTest(){
        System.out.println("Maven + Hibernate + MySQL");
        Session session = HibernateUtil.getSessionFactory().openSession();

        session.beginTransaction();
        Employee e = new Employee();
        System.out.println("Beginning transaction");
        e.setFirstName("Pritam Test Again");
        e.setLastName("Banerje");
        e.setSalary(12);

        session.save(e);
        session.getTransaction().commit();
    }

Create table query :

CREATE TABLE EMPLOYEE123 
(ID INTEGER,
FIRST_NAME VARCHAR,
LAST_NAME VARCHAR,
SALARY INTEGER);

Configuration Files can be anything standard for these kind of files. here is an example of Employee.hbm.xml. Remember to properly put the package names.

<class name="com.sow.application.Employee" table="EMPLOYEE123">
  <meta attribute="class-description">
     This class contains the employee detail. 
  </meta>
  <id name="id" type="int" column="id">
     <generator class="native"/>
  </id>
  <property name="firstName" column="first_name" type="string"/>
  <property name="lastName" column="last_name" type="string"/>
  <property name="salary" column="salary" type="int"/>
</class> 

And here is the Configuration xml file hibernate.cfg.xml:

<hibernate-configuration>
 <session-factory>
 <property name="hibernate.dialect">
   org.hibernate.dialect.SQLServerDialect
 </property>
 <property name="hibernate.connection.driver_class">
   com.microsoft.sqlserver.jdbc.SQLServerDriver
 </property>

 <property name="hibernate.connection.url">
   jdbc:sqlserver://localhost:1444 
 </property>
  <property name="hibernate.default_schema">dbo</property>
 <property name="hibernate.connection.username">
   UserName
 </property>
<property name="hibernate.connection.password">
   Password
</property>

<!-- List of XML mapping files -->
<mapping resource="com/sow/application/Employee.hbm.xml"/>

</session-factory>
</hibernate-configuration>
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108