Saturday, December 31, 2011

Getting started with HIBERNATE note 1

The best Hibernate video tutorial I have ever seen.
I have configured the user library and hibernate.cfg.xml for the MySQL.
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"



        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernatesample</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <!-- define default schema -->
        <property name="hibernate.default_schema">hibernatesample</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's current session context -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

<!--         Drop and re-create the database schema on startup
        <property name="">create</property>

        <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>
        <mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/> -->



We can identify 4 steps when we persist a class using HIBERNATE.
  1. create the class
  2. add relevant annotations for relevant classes
  3. schema exporting (creating tables)
  4. persists the classes
Creating class and add annotations
Sample Code :
package com.hibernate.ch1;

import javax.persistence.Entity;
import javax.persistence.Id;

public class Student {
 private int stdID;
 private String stdName;
 private int stdAge;
 public int getStdID() {
  return stdID;
 public void setStdID(int stdID) {
  this.stdID = stdID;
 public String getStdName() {
  return stdName;
 public void setStdName(String stdName) {
  this.stdName = stdName;
 public int getStdAge() {
  return stdAge;
 public void setStdAge(int stdAge) {
  this.stdAge = stdAge;

Annotation description :
  • @Entity : This annotation marks the class as a persist-able class.
  • @Id : We can annotate the primary key using this annotation
  • always use annotations available in javax.persistance package because those annotations are provided by sun cooperation. but HIBERNATE annotations may change in the future. so we don't depend on different ORM tools if we use javax.persistance annotations.
Schema exporting and Persisting classes
Sample code :

package com.hibernate.ch1;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class Test {

  * @param args
 public static void main(String[] args) {
  AnnotationConfiguration config=new AnnotationConfiguration();
  //new SchemaExport(config).create(true, true);
  //uncomment above line if the schema is not created yet
Code after schema exporting
  /*SessionFactory objects are very resource intensive
   * so we have to create them very carefully
  SessionFactory factory=config.buildSessionFactory();
  Session session=factory.getCurrentSession();
  Student st1=new Student();
  st1.setStdName("Kanishka Dilshan");
  Student st2=new Student();
  st2.setStdName("Sajith Athukorala");



Post a Comment

© kani.stack.notez 2012 | Blogger Template by Enny Law - Ngetik Dot Com - Nulis