Rules Of Persistent Classes by R4R Team

The four main rules of persistent classes are explored in more detail in the following sections.

1. Implement a no-argument constructor: In the above category we take a pojo class Student that has a no-argument constructor. We have to remember one thing that is all persistent classes must have a default constructor (which can be non-public) so that Hibernate can instantiate them using Constructor.newInstance(). It is recommended that have a default constructor with at least package visibility for runtime proxy generation in Hibernate.

2. Provide an identifier property (optional): Student class has a property called id. By This property maps to the primary key column of a database table. The property might have been called anything, and its type might have been any primitive type, any primitive "wrapper" type, java.lang.String or java.util.Date. If legacy database table has composite keys, then can use a user-defined class with properties of these types (see the section on composite identifiers later in the chapter.)

The identifier property is strictly optional. So can leave them off and let Hibernate keep track of object identifiers internally. We recommend that you declare consistently-named identifier properties on persistent classes and that you use a nullable (non-primitive) type. We do not recommend this, however. In fact, some functionality is available only to classes that declare an identifier property:

Transitive reattachment for detached objects (cascade update or cascade merge) “Transitive persistence”

Session.saveOrUpdate()
Session.merge()

3. Prefer non-final classes (optional): 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.

Now we can persist final classes that do not implement an interface with Hibernate. Than we not, be able to use proxies for lazy association fetching which will ultimately limit your options for performance tuning.

We need to always avoid declaring public final methods on the non-final classes. If we want to use a class with a public final method, you must explicitly disable proxying by setting lazy="false".

4. Declare accessors and mutators for persistent fields (optional): Student class declares accessor methods for all its persistent fields. Many other ORM tools directly persist instance variables. It is better to provide an indirection between the relational schema and internal data structures of the class. By default, Hibernate persists JavaBeans style properties and recognizes method names of the form getFoo, isFoo and setFoo. If required, you can switch to direct field access for particular properties.

Properties need not be declared public - Hibernate can persist a property with a default, protected or private get / set pair.
Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!