0% found this document useful (0 votes)
24 views

Maven Steps Updated

The document provides steps to create a Maven project for Hibernate. It describes how to set up the project structure, add dependencies to pom.xml, and save an entity object to the database using Hibernate.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Maven Steps Updated

The document provides steps to create a Maven project for Hibernate. It describes how to set up the project structure, add dependencies to pom.xml, and save an entity object to the database using Hibernate.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Creating Maven Project for Hibernate

Maven is dependancy management tool . It download required jar files and add it in
project's maven dependancy folder .
when we create maven project , we do not require to download jar file and adding
it in project's build path . Maven download jar files from internet and add it in
our project automatically .

1. select file menu--> select new --> select project --> type maven in filter text
=> select maven project

2. select checkbox => create simple project

3. Sepcify group id :- com.tka

4. Sepcify artifact id (project name) :- HibernateEx

5. If you do not find src/main/java folder it means your project is NOT created
correctly

To resolve this

First Solution :- update project

Right click on project name => maven => update=>select force update checkbox

Second Solution :- use only if first solution do not work

firstly close eclipse and then go to c:/users/[your username]/.m2

delete repository folder .

6.write depedencies in pom.xml file

<dependencies>

<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.4.0.Final</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version> <!-- 5.1.49 for mysql 5 -->
</dependency>

</dependencies>
7. copy given hibernate.cfg.xml file and save it inside src/main/resources folder

update this file by changing password , url , driver class , username as per
your database

e.g. for MYSQL 5 in driver class's path cj. is not present , so remove it .

8. create entity class

import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class Employee
{
// name of columns and names of this class must be same

@Id
public int eid;
public String name;
public int salary;

// define setter and getter methods


}

9. save Employee class's object's contents in database table employee .

import java.util.List;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;

import com.tka.entity.Employee;

public class HQLSaveEx {

public static void main(String[] args) {

Session session=new
Configuration().configure().addAnnotatedClass(Employee.class).buildSessionFactory()
.openSession();

Employee employee=new Employee();

employee.setEid(800);
employee.setName("saurav");
employee.setSalary(800000);

Transaction tx=session.beginTransaction();

session.save(employee);

tx.commit();
}

You might also like