Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Hibernate Hibernate Configuration
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


samar
Hibernate Configuration
1 Answer(s)      3 years and 6 months ago
Posted in : Hibernate


Explain how to configure Hibernate?


Advertisement
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;
    }

}


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
hibernate Configuration with eclpse 3.1 - Hibernate
hibernate Configuration with eclpse 3.1  Dear Sir, i m developing a program using hibernate with struts. but i am not able to configure eclipse 3.1 ide for it. so please tell me step by step configuration process
hibernate configuration with eclipse 3.1 - Hibernate
hibernate configuration with eclipse 3.1  Dear Sir, i got your mail... simple hibernate program code and with struts also. so please tell me the step by step process. i have that folder in d:/hibernate i thing
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
Eclipse configuration - Hibernate
to configure the tomcat and hibernate in eclipse?. Thanks, Anusha
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 "
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
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
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 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
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
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
Writing Hibernate Configuration Files
Writing Hibernate Configuration Files In the previous section we completed.... In this section we will write required hibernate configuration files. For this tutorial we need following Hibernate configuration files: Hibernate Configuration
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
invalid configuration
configuration Exception in thread "main" java.lang.ExceptionInInitializerError... configuration at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2241
Hibernate - Hibernate
Hibernate sessionfactory configuration  What is the process of hibernate sessionfactory configuration
Could not parse configuration: hibernate.cfg.xml
version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration... Could not parse configuration: hibernate.cfg.xml  Hi, I am getting... configuration: hibernate.cfg.xml at org.hibernate.cfg.Configuration.doConfigure
MySQL Configuration
MySQL Configuration      ... . The mysql configuration is included with The mysql 5.0 server and mysql also available for only window users. Introduction of configuration wizard The MySQL
Configure the configuration file
Configure the configuration file  How do I configure the configuration file
configuration files are used in Struts
configuration files are used in Struts  What configuration files are used in Struts
HIBERNATE
HIBERNATE  How u connects database using hibernate 3.0? Where you have written the database configuration details
hibernate
hibernate  how to impot mysql database client jar file into eclipse for hibernate configuration   Hi Friend, Please visit the following link: http://www.roseindia.net/hibernate/runninge-xample.shtml Thanks
Form Tag library configuration
In this section, you will learn about Spring form tag library and its configuration
Action Configuration - Struts
Action Configuration  I need a code for struts action configuration in XML
configuration files - Java Beginners
configuration files  i have to use configuration file in my program "listing of number of files and their modified time of directory",but problem... by user, tell me what exactly is this configuration file , what are its uses and how
 

 

 

DMCA.com