Java Persistence with Hibernate Part 3 – Create the Database and Persistent Class
Create a Database
In using Hibernate you may either create the database first and create the class we need to persist or vice-versa.
create database enrollment; create table student( student_id int, first_name varchar(40), last_name varchar(40), primary key(student_id) );
With the SQL code above, we’ve created a database named “enrollment” and a table named “student” with three columns(student_id, first_name and last_name) with student_id as our primary key(unique identifier).
Create the Eclipse Project
Again, you don’t need to have an eclipse project just adjust what is done here to your development environment.
- Create a new Java project in this case I named the project “com.tutorial.enrollment”. Image
- Add the libraries mentioned in Part 2 to the build path Image 1, Image 2
Create the Student Class
package com.tutorial.enrollment.model;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 6981987850136048701L;
private Long id;
private String firstName;
private String lastName;
public Long getId() {
return id;
}
public void setId(Long 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;
}
}
The Student class follows the JavaBeans specification with the use of private variables and public getters and setters method. Also, we tried to have the same properties in our class and columns in the student table.
Save Student.java in com.tutorial.enrollment/src/com/tutorial/enrollment/model.
Part 2 – Create the Database and Persistent Class o0o Part 4 – Create the Mapping File and Configuration File
Categories: Hibernate, Java
Hibernate, Java, Object Relational Mapping, Persistence

Nice One. Keep up the good work.
Looking forward for more from you.