Eclipse provides m2eclipse plugin to integrate Maven and Eclipse together.
Steps to create maven java project in eclipse:
In eclipse, click on File menu → New → Maven Project. Select maven-archetype-quickstart template to create java project and Click on Next button.
Now provide the group Id, artifact Id and Package. Click on Finish button. Complete directory structure and all files like Java file, pom.xml file, test case file etc will be created automatically.
Directory structure of the maven java project:
Add hibernate dependencies to automatically generated pom.xml file.
package com.w3spoint;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
SessionFactory sessionFactory = null;
try {
//Create the session factory object.
return new Configuration().configure().buildSessionFactory();
}
catch (Exception e) {
e.printStackTrace();
}
return sessionFactory;
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Modify App.java
packagecom.w3spoint;importjava.util.Date;importorg.hibernate.HibernateException;importorg.hibernate.Session;importorg.hibernate.Transaction;importcom.w3spoint.HibernateUtil;publicclass App
{publicstaticvoid main(String[] args ){//Create the student object.
Users user =new Users();//Setting the object properties.
user.setUserName("Vivek");
user.setPassword("vivek@123");
user.setCreateDate(newDate());
user.setCreateUser("Jai");
Transaction tx =null;//Get the session object.
Session session = HibernateUtil.getSessionFactory().openSession();try{//Start hibernate session.
tx = session.beginTransaction();//Insert a new student record in the database.
session.save(user);//Commit hibernate transaction if no exception occurs.
tx.commit();System.out.println("Saved Successfully.");}catch(HibernateException e){if(tx!=null){//Roll back if any exception occurs.
tx.rollback();}
e.printStackTrace();}finally{//Close hibernate session.
session.close();}}}
package com.w3spoint;
import java.util.Date;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.w3spoint.HibernateUtil;
public class App
{
public static void main( String[] args ) {
//Create the student object.
Users user = new Users();
//Setting the object properties.
user.setUserName("Vivek");
user.setPassword("vivek@123");
user.setCreateDate(new Date());
user.setCreateUser("Jai");
Transaction tx = null;
//Get the session object.
Session session = HibernateUtil.getSessionFactory().openSession();
try{
//Start hibernate session.
tx = session.beginTransaction();
//Insert a new student record in the database.
session.save(user);
//Commit hibernate transaction if no exception occurs.
tx.commit();
System.out.println("Saved Successfully.");
}catch (HibernateException e) {
if(tx!=null){
//Roll back if any exception occurs.
tx.rollback();
}
e.printStackTrace();
}finally {
//Close hibernate session.
session.close();
}
}
}