Hibernate Configuration

Ads
 

Hibernate Configuration

Explain how to configure Hibernate?

View Answers

March 7, 2011 at 3:23 PM

Hibernate configuration--- It uses a XML file. The name of this file is hibernate.cfg.xml. It is used to create connection pool or established the required environment variable. Required file for configuration (by using annotation)

Step1 - hibernate.cfg.xml ? It is a configuration file, which provides database connection.

<?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://192.168.10.13:3306/onlinexamination</property>
        <property name="hibernate.connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.pool_size">1</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="show_sql">true</property>
        <property name="current_session_context_class">thread</property>
        <property name="hbm2ddl.auto">update</property>
        <mapping class="net.roseindia.Model.AddInformation" />
    </session-factory>
</hibernate-configuration>

Step 2 -- HibernateUtil class The HibernateUtil is a java class, which provides SessionFactory from the Hibernate configuration file. It manages the Hibernate session. HibernateUtil class is a helper class that handle Hibernate's SessionFactory to obtain a Session object. This class load the hibernate.cfg.xml file using configure () method.

package net.roseindia.hibernateUtil;

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

public class HibernateUtil {
    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new AnnotationConfiguration().configure()
                    .buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

Step3- Model class

It is a POJO class. It has properties and their getter setter method. It also provides mapping of properties to table column.

package net.roseindia.Model;

import java.io.Serializable;

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

@Entity
@Table(name = "userinfo")
public class AddInformation implements Serializable {
    private String firstname;
    private int id;
    @Id
    @GeneratedValue
    @Column(name = "id")
    public int getId() {
        return id;
    }

    @Column(name = "firstname")
    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public void setId(int id) {
        this.id = id;
    }
}

Step4?DAO It is java class. This class is used for accessing data from database table. The method of this class will be called from action class.

package net.roseindia.dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.classic.Session;
import net.roseindia.Model.AddInformation;
import net.roseindia.hibernateUtil.HibernateUtil;

public class InformationDAO extends HibernateUtil {
public AddInformation add(AddInformation obInformation) {
Session session =  HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.save(obInformation);
        session.getTransaction().commit();
        return obInformation;
    }

}

Ads









Related Tutorials/Questions & Answers:
Hibernate Configuration
Hibernate Configuration  Explain how to configure Hibernate
Hibernate dialect configuration
Hibernate dialect configuration  How to configure hibernate dialect in the Hibernate configuration file? Thanks
Advertisements
Hibernate configuration file
This tutorial helps you in understanding the configuration file of Hibernate
Struts2 And Hibernate Configuration
Struts2 And Hibernate Configuration  Sir, I am new to struts2, I am trying to write a program using struts2 and hibernate. I want to know how.... Bachan Sahoo   Struts Hibernate Integration
Hibernate Configuration File
Hibernate Configuration File In this tutorial you will learn about the hibernate configuration file. To define a Hibernate Configuration info a resource file...='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "
Doctype Hibernate-configuration 5
Using Doctype in hibernate-configuration 5 In Hibernate 5 correct doctype should be used and this data is provided in the configuration file of Hibernate...; In the configuration of file of Hibernate we also provide the Database Driver class
Eclipse configuration - Hibernate
to configure the tomcat and hibernate in eclipse?. Thanks, Anusha
What are the most common methods of Hibernate configuration?
What are the most common methods of Hibernate configuration?  Hi, What are the most common methods of Hibernate configuration? thanks
Hibernate Configuration File
Hibernate Configuration File In this section we will read about the hibernate... connection information to a file. Hibernate Configuration file i.e.... of Configuration class. Hibernate configuration file provides the information
Can you provide me Hibernate configuration tutorial?
Can you provide me Hibernate configuration tutorial?  Hello There, I want to learn about Hibernate configuration, Can anyone provide me with hibernate configuration tutorial> Thanks
Hibernate 5 configuration dtd
Hibernate 5 configuration dtd Example In Hibernate 5 dtd is different from... in the hibernate.cfg.xml file. What is Hibernate Configuration file? To use Hibernate we have... of hibernate.cfg file In Hibernate 5 you should use correct dtd configuration. Following
Hibernate Configuration
The configuration of Hibernate is handled by the instance of org.hibernate.cfg.Configuration . It symbolizes the complete mapping between Java data types to the SQL database or database table. It is used to construct
How to get hibernate configuration from sessionfactory?
How to get hibernate configuration from sessionfactory?  Hi, I need to get hibernate configuration from sessionfactory, how can i do it? Thanks..., you would be able to understand how to get hibernate configuration from
Hibernate Configuration files
Hibernate Configuration files - Learn about the files in the Hibernate... the configuration files of Hibernate. As a beginner you should be aware of all... based applications. Following is the list of configuration files in Hibernate
Hibernate 5 JPA Configuration
Hibernate 5 JPA Configuration in application The JPA API depends on one... will learn to use Hibernate 5 JPA Configuration file to make fully functional... the Hibernate 5 JPA Configuration code which you can used to use Hibernate 5 and JPA
Hibernate 5 JPA Configuration
Hibernate 5 JPA Configuration in application The JPA API depends on one... will learn to use Hibernate 5 JPA Configuration file to make fully functional... the Hibernate 5 JPA Configuration code which you can used to use Hibernate 5 and JPA
Writing Hibernate Configuration Files
Writing Hibernate Configuration Files   ... we will write required hibernate configuration files. For this tutorial we need following Hibernate configuration files: Hibernate Configuration File
Spring MVC and Hibernate with DWR 3 -- Configuration Using MySQL Database
Spring MVC and Hibernate with DWR 3 -- Configuration Using MySQL Database  How to configure Spring MVC and Hibernate with DWR 3, Simple example -- reading a table, editing-deleting and adding new rows
Hibernate db2
In this section, you will learn about the db2 configuration in Hibernate
Hibernate config
In this section, you will learn about Hibernate config or configuration in Hibernate
hibernate tomcat
In this section, you will learn about Hibernate datasource configuration in Tomcat
Hibernate - Hibernate
Hibernate sessionfactory configuration  What is the process of hibernate sessionfactory configuration
HIBERNATE
HIBERNATE  How u connects database using hibernate 3.0? Where you have written the database configuration details
HIBERNATE
HIBERNATE   What is difference between Jdbc and Hibernate
hibernate
hibernate  what is hibernate listeners
hibernate
hibernate  what is hibernate flow
hibernate
hibernate  please give me the link where i can freely download hibernate software(with dependencies)   Learn hibernate, if you want to learn hibernate, please visit the following link: Hibernate Tutorials
Hibernate
Hibernate  hibernate delete query execution
hibernate
hibernate  what is hibernate why we use
Hibernate
Hibernate  how to use pagination concept in Hibernate
hibernate
hibernate   I want to learn how to use hibernate framework in web application . for storing database in our application

Ads