Hibernate Annotations & Serializable Interface



Why Hibernate entities needs to implement serializable interface do we really need it always or for some scenarios.???

As per oracle docs
"If an entity instance be passed by value as a detached object, such as through a session bean’s remote business interface, the class must implement the Serializable interface."

So its look like it is required in some scenarios but its not mandatory.So may be we take it as a best practice so it would be compatible to use all scenarios. There could be scenarios where we may have to pass JPA pojo or Entity as a value holder object from one layer to another to avoid use of DTO's (Data Transfer object). Which we use by copy & pass the data to next layers.  Some time we may need to pass them over Http layers as a session object may be from presentation layer to business layer. So its content need to be serializable. So we reached on conclusion that its better is most of cases to implement serializable marker interface.

why we have 2 types of annotations in hibernate.First which starts from javax.persistence.* & another one which are like org.hibernate.annotations.* ??????

So one type is JPA based which are javax.persistence & another one is Hibernate based which are org.hibernate.annotations. Hibernate take advantage from JPA annotations as they are applicable in all JPA compliant ORM tools.One more point is JPA annotations are basic one & hibernation annotations are providing extra feature.So one should first with JPA & if required should move towards Hibernate annotations.

JPA Basic annotations: 

@Entityimport javax.persistence.Entity;
@Tableimport javax.persistence.Table;
@Columnimport javax.persistence.Column;
@Idimport javax.persistence.Id;
@GeneratedValueimport javax.persistence.GeneratedValue;
@Versionimport javax.persistence.Version;
@OrderByimport javax.persistence.OrderBy;
@Transientimport javax.persistence.Transient;
@Lobimport javax.persistence.Lob;

JPA Mapping annotations 

@OneToOneimport javax.persistence.OneToOne;
@ManyToOneimport javax.persistence.ManyToOne;
@OneToManyimport javax.persistence.OneToMany;
@ManyToManyimport javax.persistence.ManyToMany;
@PrimaryKeyJoinColumnimport javax.persistence.PrimaryKeyJoinColumn;
@JoinColumnimport javax.persistence.JoinColumn;
@JoinTableimport javax.persistence.JoinTable;
@MapsIdimport javax.persistence.MapsId;


JAP inheritance annotations

@Inheritanceimport javax.persistence.Inheritance;
@DiscriminatorColumnimport javax.persistence.DiscriminatorColumn;
@DiscriminatorValueimport javax.persistence.DiscriminatorValue;



While above JPA basic annotations is good to be used in simple application Hibernate annotations are present for some advanced user or applications.


Hibernate annotations for Queries

@org.hibernate.annotations.NamedQuery
@org.hibernate.annotations.NamedQueries,
@org.hibernate.annotations.NamedNativeQuery
 @org.hibernate.annotations.NamedNativeQueries

As a Query Hints

org.hibernate.cacheableWhether the query should interact with the second level cache (defualt to false);
org.hibernate.cacheRegionCache region name (default used otherwise)
org.hibernate.timeoutQuery timeout
org.hibernate.fetchSizeresultset fetch size
org.hibernate.flushModeFlush mode used for this query
org.hibernate.cacheModeCache mode used for this query
org.hibernate.readOnlyEntities loaded by this query should be in read only mode or not (default to false)
org.hibernate.commentQuery comment added to the generated SQL



Hibernate annotations for Runtime filters 

@org.hibernate.annotations.FilterDef or @FilterDefs define filter definition(s) used by filter(s) using the same name. A filter definition has a name() and an array of parameters(). A parameter will allow you to adjust the behavior of the filter at runtime
@FilterDef(name="minLength", parameters=@ParamDef( name="minLength", type="integer" ) )
@Filters( {
    @Filter(name="betweenLength", condition=":minLength <= length and :maxLength >= length"),
    @Filter(name="minLength", condition=":minLength <= length")
} )


annotaions for Custom SQL for CRUD operations

@SQLInsert@SQLUpdate@SQLDelete@SQLDeleteAll respectively override the INSERT statement, UPDATE statement, DELETE statement, DELETE statement to remove all entities.

annotations for Fetch profiles

@Entity
@FetchProfile(name = "customer-with-orders", fetchOverrides = {
   @FetchProfile.FetchOverride(entity = Customer.class, association = "orders", mode = FetchMode.JOIN)
})
public class Customer {
   @Id
   @GeneratedValue
   private long id;
   private String name;
   private long customerNumber;
   @OneToMany
   private Set<Order> orders
}



There are other modules of hibernate exist where other annotations available. 
Hibernate Validator
Hibernate search 
Hibernate Entity Manager
Hibernate Shards
Hibernate tools




Comments

Popular posts from this blog

Conversion from Clob To String in java and Vice versa

Portable Java 8/ JDK8 setup without admin rights

Multi Threading , Producer consumer problem in Java