Hibernate App Development
Development Steps
1. Create a Simple Maven Project
2. Project Directory Structure
3. Add jar Dependencies to [Link]
4. Creating the JPA Entity Class(Persistent class)
5. Create a Hibernate configuration file - [Link]
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: [Link]
• 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 [Link].
<build>
<plugins>
<plugin>
<groupId>[Link]</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 [Link] file, update the maven project. To update maven project right click on maven-project → Maven → Update Project.
Step-6
Add some dependencies to [Link] file. Here we are adding junit dependency to [Link].
<project
xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link] [Link]
<modelVersion>4.0.0</modelVersion>
<groupId>[Link]-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>[Link]</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 [Link], under src/test/java folder. Now create a class [Link] under src/test/java package and write the following code in it.
package [Link];
import [Link];
import [Link];
public class AppTest {
@Test
public void test() {
[Link]("Hello Maven", new String("Hello Maven"));
}
}
Step-8
Run your first maven project. Right click on [Link] → Run as → JUnit Test.
2. Project Directory Structure
3. Add jar Dependencies to [Link]
Visit Maven Repository: Search/Browse/Explore ([Link])
Java_ Page 3
<project
xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link] [Link]
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>[Link]</groupId>
<artifactId>hibernate-tutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>hibernate-java-config-example</artifactId>
<properties>
<[Link]>UTF-8</[Link]>
</properties>
<dependencies>
<!-- [Link] -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
<!-- [Link] -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>hibernate-core</artifactId>
<version>[Link]</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 [Link];
import [Link].*;
@Entity
@Table(name = "student")
public class Student
{
@Id
@GeneratedValue(strategy = [Link])
@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)
{
[Link] = firstName;
[Link] = lastName;
[Link] = email;
}
public int getId() {
return id;
}
public void setId(int id) {
[Link] = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
[Link] = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
[Link] = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
[Link] = email;
Java_ Page 5
[Link] = email;
}
@Override
public String toString()
{
return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
}
}
Java_ Page 6