Hibernate Annotations Introduction

Gun short point :-  Annotations are replacement for XML

Let us see few points regarding annotations in hibernate

  • Annotations are introduced in java along with JDK 1.5, annotations are used to provide META data to the classes, variables, methods of java
  • Annotations are given by SUN as replacement to the use of xml files in java
  • In hibernate annotations are given to replace hibernate mapping [ xml ] files
  • While working with annotations in hibernate, we do not require any mapping files, but hibernate xml configuration file is must
  • hibernate borrowed annotations from java persistence API but hibernate it self doesn’t contain its own annotations
  • Every annotations is internally an Interface, but the key words starts with @ symbol

Like…..

1234public @interface Java4s
{
    // code.....
}
  • For working with annotations, our java version must be 1.5 or higher and hibernate version must be hibernate 3.3 or higher, actually up to now we have been used hibernate 2.2 in our previous tutorials, get ready to download the higher version ?
  • At j2se level, sun has provided very limited set of annotations like @Override and @Deprecated …etc…
  • Sun has provided the annotations related to j2se level under java.lang.annotations.* package
  • Most of the annotations related to j2ee level are given by sun and their implementations are given by the vendors
  • In hibernate, the annotations supported are given by sun under javax.persistence package and hibernate has provided implementations for annotations given in that package

The basic annotations we are using while creating hibernate pojo classes are…

  • @Entity
  • @Table
  • @Id
  • @Column

Actually @Entity, @Table are class level annotations, and @Id, @Column are the field level annotations, no worries you will be able to understand while seeing the first example ?

  • in hibernate, if we are annotations in the pojo class then hibernate mapping file is not required, it means annotations are reducing the use of xml files in the hibernate

Though we are using annotations in our pojo class with mapping xml also, then hibernate will give first preference to xml only not for annotations,  actually this concept is same in struts, hibernate, spring too

And that’s it friends,  now you are ready to work with annotations in hibernate ?

 

Related Articles

post a comment