Session
the Session object is used to obtain a physical connection to the database. The session object is lightweight and designed an interaction that needs to be instantiated with the database each time. The persisted object is saved and retrieved through a Session object.
The objects in the session should not remain open for a long time because they are usually not thread-safe and they should be created and destroyed as needed. The main function of this meeting is to provide an instance of an entity class that creates, reads, and deletes an action map. There may be one of the following three states in the instance at a given point in time:
Ephemeral: The persistent class is not associated with the session and is not representative in the database, and a new instance without an identity value is considered temporary by hibernate.
Persistence: A transient instance can be persisted by associating it with a session. Persistence instances have a representation in the database, an identifier value, which is associated with the session.
Independence: Once we close the session of hibernate, the persisted instance becomes a detached instance.
A session instance is serializable if its persisted class is serializable. A typical transaction should 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 the session throws an exception, the transaction must be rolled back and the conversation must be discarded.
Persistence class
the whole concept of hibernate is to take the value from the Java class attribute and will persist to the database table. A mapping file Hibernate help determine how to pull the values from the class and map them to the table and related fields.
Java classes whose objects or instances are stored in a database table are called persistent classes in Hibernate. Hibernate works best if these classes follow some simple rules, also known as the normal Java object (POJO) programming model. There are primary rules for the following persisted classes, but these rules are not required.
A default constructor is required for all of the persisted Java classes.
All classes should contain IDs that are easy to identify hibernate and databases within an object. This property maps to the primary key column of a database table.
All attributes that will be persisted should be declared private and have been defined in the JavaBean style by the getxxx and Setxxx methods.
The key function of Hibernate, the proxy, depends on the implementation of the interface of the persistent class or of a non final, or declared, public method.
All classes that do not extend or implement the EJB framework require some specialized classes and interfaces.
The Pojo name is used to emphasize that a given object is a normal Java object, rather than a special object, better than Enterprise JavaBean.
A simple example of 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;
}
}