Eclipse maven hibernate project:
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.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>w3schools</groupId>
<artifactId>MavenHibernateProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MavenHibernateProject</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- JUnit 3.8.1 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Hibernate 4.3.6 Final -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.6.Final</version>
</dependency>
<!-- Mysql Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
</dependencies>
</project> |
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>w3schools</groupId>
<artifactId>MavenHibernateProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MavenHibernateProject</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- JUnit 3.8.1 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Hibernate 4.3.6 Final -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.6.Final</version>
</dependency>
<!-- Mysql Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
</dependencies>
</project>
Create hibernate.cfg.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test_schema</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">pannu#519699</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping class="com.tutorialspointexamples.Users"></mapping>
</session-factory>
</hibernate-configuration> |
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test_schema</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">pannu#519699</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping class="com.tutorialspointexamples.Users"></mapping>
</session-factory>
</hibernate-configuration>
Create an entity class Users.java:
package com.w3schools;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name= "users")
public class Users {
private int seq;
private String userName;
private String password;
private Date createDate;
private String createUser;
private Date modifyDate;
private String modifyUser;
public Users(){
}
@Id
@Column(name = "SEQ", unique = true, nullable = false)
public int getSeq() {
return seq;
}
public void setSeq(int seq) {
this.seq = seq;
}
@Column(name = "USER_NAME")
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(name = "PASSWORD")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name = "CREATE_DATE")
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
@Column(name = "CREATE_USER")
public String getCreateUser() {
return createUser;
}
public void setCreateUser(String createUser) {
this.createUser = createUser;
}
@Column(name = "MODIFY_DATE")
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
@Column(name = "MODIFY_USER")
public String getModifyUser() {
return modifyUser;
}
public void setModifyUser(String modifyUser) {
this.modifyUser = modifyUser;
}
} |
package com.w3schools;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name= "users")
public class Users {
private int seq;
private String userName;
private String password;
private Date createDate;
private String createUser;
private Date modifyDate;
private String modifyUser;
public Users(){
}
@Id
@Column(name = "SEQ", unique = true, nullable = false)
public int getSeq() {
return seq;
}
public void setSeq(int seq) {
this.seq = seq;
}
@Column(name = "USER_NAME")
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(name = "PASSWORD")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name = "CREATE_DATE")
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
@Column(name = "CREATE_USER")
public String getCreateUser() {
return createUser;
}
public void setCreateUser(String createUser) {
this.createUser = createUser;
}
@Column(name = "MODIFY_DATE")
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
@Column(name = "MODIFY_USER")
public String getModifyUser() {
return modifyUser;
}
public void setModifyUser(String modifyUser) {
this.modifyUser = modifyUser;
}
}
Create Hibernate Utility class HibernateUtil.java
package com.w3schools;
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;
}
} |
package com.w3schools;
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
package com.w3schools;
import java.util.Date;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.w3schools.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();
}
}
} |
package com.w3schools;
import java.util.Date;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.w3schools.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();
}
}
}
Output
Hibernate:
insert into users (CREATE_DATE, CREATE_USER, MODIFY_DATE, MODIFY_USER, PASSWORD, USER_NAME, SEQ) values (?, ?, ?, ?, ?, ?, ?)
Saved Successfully. |
Hibernate:
insert into users (CREATE_DATE, CREATE_USER, MODIFY_DATE, MODIFY_USER, PASSWORD, USER_NAME, SEQ) values (?, ?, ?, ?, ?, ?, ?)
Saved Successfully.