Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Java Hibernate Cookbook

You're reading from   Java Hibernate Cookbook Over 50 recipes to help you build dynamic and powerful real-time Java Hibernate applications

Arrow left icon
Product type Paperback
Published in Sep 2015
Publisher
ISBN-13 9781784391904
Length 250 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Table of Contents (10) Chapters Close

Preface 1. Setting Up Hibernate FREE CHAPTER 2. Understanding the Fundamentals 3. Basic Annotations 4. Working with Collections 5. Working with Associations 6. Querying 7. Advanced Concepts 8. Integration with Other Frameworks Index

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 and Setter methods, such as getXXX and setXXX where xxx 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:

Employee

id

firstName

salary

  1. 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, and salary)

There's more…

Now, let's see how to design a POJO for tables having references between the Department and Employee tables:

There's more…

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.

You have been reading a chapter from
Java Hibernate Cookbook
Published in: Sep 2015
Publisher:
ISBN-13: 9781784391904
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image