Saturday, 9 June 2012

Hibernate Persistent Class

The entire concept of Hibernate is to take the values from Java class attributes and persist them to a database table. A mapping document helps Hibernate in determining how to pull the values from the classes and map them with table and associated fields.

Java classes whose objects or instances will be stored in database tables are called persistent classes in Hibernate. Hibernate works best if these classes follow some simple rules, also known as the Plain Old Java Object (POJO) programming model. There are following main rules of persistent classes, however, none of these rules are hard requirements.

All Java classes that will be persisted need a default constructor.

All classes should contain an ID in order to allow easy identification of your objects within Hibernate and the database. This property maps to the primary key column of a database table.

All attributes that will be persisted should be declared private and have getXXX and setXXX methods defined in the JavaBean style.

A central feature of Hibernate, proxies, depends upon the persistent class being either non-final, or the implementation of an interface that declares all public methods.

All classes that do not extend or implement some specialized classes and interfaces required by the EJB framework.

The POJO name is used to emphasize that a given object is an ordinary Java Object, not a special object, and in particular not an Enterprise JavaBean.

A simple POJO example:

Based on the few rules mentioned above we can define a POJO class as follows:


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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;
 }
}

it is possible to insert record in between the two records?

A table is an unordered recordset. The position of a record in the table is not relevant unless you impose an order on it. That is quite different from a spreadsheet, as you suggest.

If the order is important, you will need to apply the order in a query or in a form, not in the table.

You can't always get what you want, but if you try sometimes you may just get what you need - Rolling Stones
Related Posts Plugin for WordPress, Blogger...