Home > Hibernate, Java > Java Persistence with Hibernate Part 3 – Create the Database and Persistent Class

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.

  1. Create a new Java project in this case I named the project “com.tutorial.enrollment”. Image
  2. 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

Advertisement
  1. Jessica Alba
    August 15, 2009 at 1:47 am | #1

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

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.