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>