Hibernate Hello World Program With Annotations

Folks we will see one simple program with hibernate annotations, let us take inserting a record [ saving one object ] into the database application.  And remember in the annotations no need to write mapping xml, hope you remember the previous sessions ?

Files required..

  • Product.java [ our pojo class ]
  • ClientForSave.java
  • hibernate.cfg.xml

Product.java

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748package str;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "student_talbe")
public class Product{

	@Id
	@Column(name="proid")
	private int productId;

	@Column(name="proName", length=10)
	private String proName;

	@Column(name="price")
	private double price;

	public void setProductId(int productId)
	{
	    this.productId = productId;
	}
	public int getProductId()
	{
	    return productId;
	}

	public void setProName(String proName)
	{
	    this.proName = proName;
	}
	public String getProName()
	{
	    return proName;
	}

	public void setPrice(double price)
	{
	    this.price = price;
	}
	public double getPrice()
	{
	    return price;
	}
}

ClientForSave.java

1234567891011121314151617181920212223242526272829303132package str;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

public class ClientForSave { 

    public static void main(String[] args)
    {

        AnnotationConfiguration cfg=new AnnotationConfiguration();
        cfg.configure("hibernate.cfg.xml");

        SessionFactory factory = cfg.buildSessionFactory();
        Session session = factory.openSession();
        Product p=new Product(); 

        p.setProductId(105);
        p.setProName("java4s");
        p.setPrice(15000);

        Transaction tx = session.beginTransaction();
        session.save(p);
        System.out.println("Object saved successfully using annotations.....!!");
        tx.commit();
        session.close();
        factory.close();
    }

}

hibernate.cfg.xml

123456789101112131415161718192021<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">jdbc:oracle:thin:@www.java4s.com:1521:XE</property>
<property name="connection.username">system</property>
<property name="connection.password">admin</property>

<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>

<mapping class="str.Product" />

</session-factory>
</hibernate-configuration>

And friends,  no need to explain there i think.  Please refer previous introduction sessions on annotations in case  you have any doubt.

 

Related Articles

post a comment