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?
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.
You do not need Maven to use Hibernate with Eclipse.
How-to on SO: Can anyone recommend a good reference for setting up Hibernate3 with Eclipse?
More Resources:
My Favorite: http://www.laliluna.de/articles/first-hibernate-example-tutorial.html
http://www.skill-guru.com/blog/2009/08/05/first-hibernate-tutorial-%E2%80%93get-hands-on-experience/#more-259
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.
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.
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
- Download Hibernate 5 from here.
- 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
. 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>
精彩评论