Tuesday, September 20, 2011

Hibernate "Hello World" with and without Annotation

Without Annotation
1.    Entity should be a normal POJO class and there is no need to implement Persistance Interface
2.    There should be a mapping.xml file for each Entity, Standard for name is Entity.hbm.xml
3.    Main tag in hbm.xml file is <hibernate-mapping>
4.    You can define multiple table mappings in a single file like
<class name="com.persistance.beans.Message" table="MESSAGES">
5.    For Primary key column tag should be like this
<id name="id" column="MESSAGE_ID">
<generator />
6.    For other properties tags should be
<property name="message" column="MESSAGE_TEXT" />
 7.    If there is a foreign key we can define it like
<many-to-one name="nextMessage" cascade="all" column="NEXT_MESSAGE_ID"
foreign-key="FK_NEXT_MESSAGE" />

8.    Name of Configuration file is normally hibernate.cfg.xml or hibernate.properties
9.    In hibernate.cfg.xml we define
10.   <mapping resource="Message.hbm.xml" /> is used to define details of mapping file
11.    Main class code shoud look some thing like this
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session  = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Message message = new Message("Hello World");
Long msgId = (Long) session.save(message);

Using Annotation
1.    If we are using Annotation then we need to Annotate class with proper Annotation

2.    For Primary Key Annotation should be like
@Id @GeneratedValue
@Column(name = "MESSAGE_ID")

3.    For Column
@Column(name = "MESSAGE_TEXT")
4.    For Foreign key
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "NEXT_MESSAGE_ID")

5.    If we are using Annotation then we have to put persistance.xml file under META-INF directory
6.    There is no need of .hbm.xml file for every entity
7.    hibernate.cfg.xml file is also not required as Hibernate can automatically detect classes with annotation
8.    In persistance.xml we need to define properties related to connection pool
<persistence-unit name="persistanceworld">

9.    In Main class code should look some thing like this
EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistanceworld");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
AnnotationMessage message = new AnnotationMessage("Hello World");


  1. internet casino games
    absolutely nothing internet casino games a lot occurred to it. Almost all of the

  2. roulette
    Hey, can I use your entry on my website with a linkback?

  3. Just Me
    Nice web site you have here. Surprised you find time to maintain this so up to date. I have trouble with my personal web site.

  4. I couldnt currently have asked for a better blog. You happen to be always at hand to present excellent information, going straight away to the point for straightforward understanding of your readers. Youre really a terrific pro in this area. Thanks for always being there for people like me.