- Java Hibernate Cookbook
- Yogesh Prajapati Vishal Ranapariya
- 532字
- 2021-07-16 19:59:46
Creating a hibernate persistent class
As discussed in the Preface, the developer will be dealing with objects at every step of development. Also, when we use hibernate, we don't need to work on a core SQL query. Here, we will create a POJO (Plain Old Java Object) in Java, which represents a table in the database.
Getting ready
By POJO, we mean that we will create a Java class that satisfies the following requirements:
- It needs to have a default constructor that is persistent.
- It should contain the
id
attribute. ID is used to identify the object and is mapped with the primary column of a table. - All attributes should have
Getter
andSetter
methods, such asgetXXX
andsetXXX
wherexxx
is a field name.
How to do it...
We will now create a persistent class and name it Employee
. The following table shows a representation of the Employee
class:

- Create the
Employee.java
class and place the following code in the class:public class Employee{ private long id; private String firstName; private double salary; // other fields // default constructor public Employee() { } 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 double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } // // Getter and setter for other fields... // }
Now the preceding class satisfies all the requirements listed before to be a persistent class.
The preceding class now contains the following:
- The default
Employee()
constructor - The
id
attribute, which is the primary column of the table and can be used to uniquely identify an entry - The individual getters and setters in all the attributes (
id
,firstName
, andsalary
)
There's more…
Now, let's see how to design a POJO for tables having references between the Department
and Employee
tables:

The following code is the definition for the Department
class in Department.java
:
public class Department{ private long id; private String deptName; //default constructor public void Department(){ } //getters and setters public long getId() { return id; } public void setId(long id) { this.id = id; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } }
The following code is the definition for the Employee
class in Employee.java
:
public class Employee{
private long id;
private String firstName;
private double salary;
private Department department; // reference to Department.
//default constructor
public void Employee(){
}
//getters and setters
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 double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Department getDepartment(){
return department;
}
public setDepartment(Department department){
this.department = department;
}
}
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
- Practical Data Science Cookbook(Second Edition)
- MATLAB應用與實驗教程
- INSTANT Mercurial SCM Essentials How-to
- Kali Linux Wireless Penetration Testing Beginner's Guide(Third Edition)
- 大數據分析與應用實戰:統計機器學習之數據導向編程
- Developing SSRS Reports for Dynamics AX
- JavaScript應用開發實踐指南
- Illustrator CS6設計與應用任務教程
- Python 3 數據分析與機器學習實戰
- C編程技巧:117個問題解決方案示例
- 深入分析GCC
- 從零開始學Unity游戲開發:場景+角色+腳本+交互+體驗+效果+發布
- Raspberry Pi開發實戰
- 川哥教你Spring Boot 2實戰
- C語言從入門到精通(第5版)