Introduction to Session and persistence classes in Java's Hibernate framework

  • 2020-04-01 04:30:31
  • OfStack

The Session
The Session object is used to get a physical connection to the database. The Session object is lightweight and designed for an interaction that is needed with the database each time it is instantiated. The persistent object is saved and retrieved through a Session object.

Objects in a session should not be left open for a long time, because they are not usually thread-safe, and they should be created and destroyed as needed. The main function of this meeting is to provide instances of entity classes that are mapped to create, read, and delete operations. One of the following three states may exist in the instance at a given point in time:

Transience: a new instance of a persistent class that is not associated with the session and is not representative in the database and has no identity value is considered transient by Hibernate.

Persistence: you can do transient instance persistence by associating it with a session. Persistent instances all have a representation in the database, an identifier value, associated with the session.

Independence: once we close Hibernate's Session, the persistent instance becomes a separate instance.

A Session instance is serializable if its persistent class is serializable. A typical transaction would use the following statement:


Session session = factory.openSession();
Transaction tx = null;
try {
  tx = session.beginTransaction();
  // do some work
  ...
  tx.commit();
}
catch (Exception e) {
  if (tx!=null) tx.rollback();
  e.printStackTrace(); 
}finally {
  session.close();
}

If a Session throws an exception, the transaction must be rolled back and the Session must be discarded.

The persistent classes
The whole concept of Hibernate is to take the value of a property from a Java class and persist it to a database table. A mapping file, Hibernate, helps you determine how to pull values from classes and map them to tables and related fields.

Its object or instance calls a Java class stored in a database table a persistent class in Hibernate. Hibernate works best if these classes follow some simple rules, also known as the plain Java object (POJO) programming model. There are the following main rules for persisting classes, but they are not required.

Persisting all Java classes requires a default constructor.

All classes should contain ids to make it easy to identify Hibernate and the database within the object. This property maps to the primary key column of the database table.

All properties will be persisted should be declared private and have been defined in javabean-style getXXX and setXXX methods.

Hibernate's key functionality, the proxy, depends on the implementation of persistent classes or interfaces to all public methods that are not final, or declared.

All classes that do not extend or implement the EJB framework require specialized classes and interfaces.

POJO names are used to emphasize that a given object is an ordinary Java object, not a special object, and certainly not an Enterprise javabeans.

A simple example of a POJO:
Based on some of the rules mentioned above, we can define a POJO class as follows:


public class Employee {
  private int id;
  private String firstName; 
  private String lastName;  
  private int salary; 

  public Employee() {}
  public Employee(String fname, String lname, int salary) {
   this.firstName = fname;
   this.lastName = lname;
   this.salary = salary;
  }
  public int getId() {
   return id;
  }
  public void setId( int id ) {
   this.id = id;
  }
  public String getFirstName() {
   return firstName;
  }
  public void setFirstName( String first_name ) {
   this.firstName = first_name;
  }
  public String getLastName() {
   return lastName;
  }
  public void setLastName( String last_name ) {
   this.lastName = last_name;
  }
  public int getSalary() {
   return salary;
  }
  public void setSalary( int salary ) {
   this.salary = salary;
  }
}


Related articles: