Hibernate Demo Annotation Example

Explained below is the steps for creating a sample of hibernate based project using annotations.

Project Details

Project Structure


File Details

Product.javaPojo class for Product.
hibernate.cfg.xmlHibernate Configuration File.
Main.javaIt will control the overall process.

Java Code

Product.java

package demo.annotation;

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

@Entity
@Table(name="Product")
public class Product
{
            @Id
            @Column(name = "productId")
            @GeneratedValue
            private int productId;

            @Column(name = "productName")
            private String productName;

            @Column(name = "productPrice")
            private int productPrice;
           
            public int getProductId() {
                        return productId;
            }
            public void setProductId(int productId) {
                        this.productId = productId;
            }
            public String getProductName() {
                        return productName;
            }
            public void setProductName(String productName) {
                        this.productName = productName;
            }
            public int getProductPrice() {
                        return productPrice;
            }
            public void setProductPrice(int productPrice) {
                        this.productPrice = productPrice;
            }
}


hibernate.cfg.xml

<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
         <property name="hibernate.connection.url">jdbc:mysql://localhost/mydb</property>
        <property name="hibernate.connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">create</property>
        <property name="show_sql">true</property>
        <mapping class="demo.annotation.Product"/>
    </session-factory>
</hibernate-configuration>

Main.java

package demo.simple;

import org.hibernate.cfg.*;
import org.hibernate.*;

public class Main
{
    public static void main(String[] args)
    {
        Configuration config = new Configuration().configure("demo\\simple\\hibernate.cfg.xml");
        SessionFactory myFactory = config.buildSessionFactory();
        Session session = myFactory.openSession();
        Transaction tx= session.beginTransaction();
        Product product = new Product();
        product.setProductName("Monitor");
        product.setProductPrice(15000);
        session.save(product);
        tx.commit();
        session.close();
    }
}

Comments

Popular posts from this blog

Index MySQL datadase table in Solr

Hibernate Sharding Example

Shallow Copy