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

Hibernate App Development

Uploaded by

SakethBharadwaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Hibernate App Development

Uploaded by

SakethBharadwaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Hibernate App Development

Development Steps
1. Create a Simple Maven Project

2. Project Directory Structure

3. Add jar Dependencies to pom.xml

4. Creating the JPA Entity Class(Persistent class)

5. Create a Hibernate configuration file - hibernate.cfg.xml

6. Create a Hibernate utility class

7. Create the Main class and Run an Application

1. Create a Simple Maven Project


Tools and technologies used
• Eclipse IDE

• Maven - 3.5.3

• JDK - 1.8

Let's create step by step a simple maven project in Eclipse IDE.

Step-1
• Open Eclipse

• Click on File -> New -> Maven Project

Step-2
Click on Checkbox for both

• Create a simple project

• Use default Workspace location

• Click on next button

Java_ Page 1
Step-3
Provide GroupId and ArtifactId in next screen.

• GroupId: net.tr.hibdemo

• Artifact Id: hib-demo-project

• Name: hib-demo-project

• Description: Simple Hib Demo Project

Step-4
And you are all set. You should see a new Project in Eclipse with below structure.

Java_ Page 2
Step-5
As you can see in the maven project structure, the default java compiler version ( i.e. source and target setting ) is 1.5. To change the default settings, add the following snippet to pom.xml.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

(Optional steps)

After changes in pom.xml file, update the maven project. To update maven project right click on maven-project → Maven → Update Project.

Step-6
Add some dependencies to pom.xml file. Here we are adding junit dependency to pom.xml.
<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>net.tr.maven-demo</groupId>
<artifactId>maven-demo-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Simple Maven Demo Project</description>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

Let's test our created project by creating a simple JUnit test.

Step-7
Create a package, named as net.tr.simpleproject, under src/test/java folder. Now create a class AppTest.java under src/test/java package and write the following code in it.

package net.tr.simpleproject;

import org.junit.Assert;
import org.junit.Test;

public class AppTest {


@Test
public void test() {
Assert.assertEquals("Hello Maven", new String("Hello Maven"));
}
}

Step-8
Run your first maven project. Right click on AppTest.java → Run as → JUnit Test.

2. Project Directory Structure

3. Add jar Dependencies to pom.xml


Visit Maven Repository: Search/Browse/Explore (mvnrepository.com)

Java_ Page 3
<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>
<parent>
<groupId>net.tr.hibernate</groupId>
<artifactId>hibernate-tutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>hibernate-java-config-example</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.7.Final</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>

Java_ Page 4
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

4. Creating the JPA Entity Class(Persistent class)

A simple Persistent class should follow some rules:

• A no-arg constructor: It is recommended that you have a default constructor at least package visibility so that hibernate can create the instance o f the Persistent class by

the newInstance() method.

• Provide an identifier property: It is better to assign an attribute as id. This attribute behaves as a primary key in a database.

• Declare getter and setter methods: The Hibernate recognizes the method by getter and setter method names by default.

• Prefer non-final class: Hibernate uses the concept of proxies, which depends on the persistent class. The application programmer will not be able to use proxies for lazy

association fetching.

package net.tr.hibdemo.entity;
import jakarta.persistence.*;
@Entity
@Table(name = "student")
public class Student
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
public Student()
{
}
public Student(String firstName, String lastName, String email)
{
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;

Java_ Page 5
this.email = email;
}
@Override
public String toString()
{
return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
}
}

Java_ Page 6

You might also like