Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Persistent Class in Hibernate

Persistent Classes in Hibernate are used for persisting the application data into database, it just acts as a bridge.  All the instances of the persistent class cant be in persistent state.

Fig: Persistent Class State representation
We will see later when'll persistent object shouldn't act as persistent state.

Tips to write Persistent class:
Persistent class is a normal POJO class.While writing POJO class we must follow some rules in hibernate.




1) Implement a no argument constructor, zero-arg constructor(non public) is required  for creating the instance using Constructor.createInstance();

  Public class Hibernate
{
    private Hibernate(){}   // zero-arg constructor
}

2) Provide an identifier value, this property of a class can be used to map the primary key column of the table.

     private Long hibernateId;  // represents pk.

3) Prefer non-final classes as POJO,The main feature of Hibernate is generating the proxy of Persistent class.If we go for JDK proxies then go for interface of a class which has public methods for creating the proxy, or if we go for CGLIB proxies then go for non-final classes so that we can subclass the class for proxy generation.

Go to this link for more information on Proxy creation.

4) Declare accessors and mutators (setter / getter pair) for the coloumns of the table.
Fig: Relation between class and table



5) Implementing equals() and hashcode() methods, If we want to make detached object to persistent object then we must implement equals() and hashcode() methods.

Finally a persistent class should look like

public class ProjectDetails
{
   private Long projectId;
   private Stirng projectName;
   private String projectManager;
   private String allocationStatus;

   private ProjectDetails()
   {}
  // setters and getters

}
                                                                                                     Vissu 


This post first appeared on Java Spring Hibernate, please read the originial post: here

Share the post

Persistent Class in Hibernate

×

Subscribe to Java Spring Hibernate

Get updates delivered right to your inbox!

Thank you for your subscription

×