Share on Google+Share on Google+

4 Answer(s)      8 years ago
Posted in : Hibernate


View Answers

September 19, 2008 at 4:00 PM

Hi jagadhish,

package hibernateexample;

import javax.transaction.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

* @author Administrator
public class HibernateExample {

* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub
Session session = null;
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
org.hibernate.Transaction tr = session.beginTransaction();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setEmail("[email protected]");
System.out.println("Insert record for second table");
User user = new User();
catch(Exception e){
// Actual contact insertion will happen at this step

September 19, 2008 at 4:02 PM

package hibernateexample;

public class Contact {

private String firstName;
private String lastName;
private String email;
private int id;

public String getEmail() {
return email;
public void setEmail(String email) { = email;
public String getFirstName() {
return firstName;
public void setFirstName(String firstName) {
this.firstName = firstName;
public String getLastName() {
return lastName;
public void setLastName(String lastName) {
this.lastName = lastName;
public int getId() {
return id;
public void setId(int id) { = id;


package hibernateexample;

public class User {

private String username;
private String phoneNo;
private int id;

public String getUsername() {
return username;
public void setUsername(String username) {
this.username = username;

public String getPhoneNo(){
return phoneNo;

public void setPhoneNo(String phoneNo){
this.phoneNo = phoneNo;

public int getId(){
return id;

public void setId(int id){ = id;

September 19, 2008 at 4:03 PM


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

<class name="hibernateexample.Contact" table="CONTACT">
<id name="id" type="int" column="ID" >
<generator class="assigned"/>

<property name="firstName">
<column name="FIRSTNAME" />
<property name="lastName">
<column name="LASTNAME"/>
<property name="email">
<column name="EMAIL"/>

<class name="hibernateexample.User" table="USER">
<id name="id" type="int" column="ID" >
<generator class="assigned"/>

<property name="username">
<column name="username" />
<property name="phoneNo">
<column name="phoneNo"/>



September 19, 2008 at 4:07 PM


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://</property>;
<property name="hibernate.connection.username">amar</property>
<property name="hibernate.connection.password">amar123</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="">update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>


Visit for more information.



Related Tutorials/Questions & Answers:
Java Hibernate
Java Hibernate  Sir, I want to learn hibernate but i don't know JSP. How & from Where i can start to learn hibernate?   Hello Friend, Please go through the following link: Hibernate Tutorials Here you will get
JAVA - Hibernate
: Primary feature of hibernate is to java classes to database tables. Data query... is flexible and powerful ORM solution to map Java classes to database tables. Hibernate...JAVA  hello friends please answer me. 1. what is hibernate...? 2
Hibernate in java - Hibernate
Hibernate in java  Hi, This is Gopi, I am working... and Hibernate). Is there any body who can help me out in my task. All I want is an example in Hibernate. " How to configure a login page (user name and password
java(Hibernate) - Hibernate
java(Hibernate)  Hai Amardeep This is jagadhish.Iam giving full code... will read hibernate.cfg.xml and prepare hibernate for use SessionFactory... for more information. Thanks Amardeep
java - Hibernate
java  how can we configure many to many relationships in hibernate
java - Hibernate
java  HI guys can any one tell me,procedure for executing spring and hibernate in myeclipse ide,plz very urgent for me,thank's in advance.  .../hibernate/runninge-xample.shtml Thanks
java - Hibernate
the code and run using Hibernate and annotation..plse help me...  Hi friend, Read for more information. Thanks
java - Hibernate
java  tell me how to configure hibernate in eclipse,where i have to add jar files and how can i get the jar files please tell me the clear...:// Thanks
Java - Hibernate
, this type of output. ---------------------------- Inserting Record Done Hibernate... = null; try{ // This step will read hibernate.cfg.xml and prepare hibernate... for more information . Thanks
java - Hibernate
java  hello i am Ramya.... i doing a project on j2ee using hibernate.... i hav created a database name employee... i tat i hav a table something called user.... in tat i hav some three fields lik name , status and... so nw my
What is hibernate technology in java?
What is hibernate technology in java?  What is hibernate technology in java?   Hibernate: -Hibernate is an Open Source persistence... impedance mismatch problems. For Hibernate detail click here
java - Hibernate
Java application development Help  Java application developmentHi all, i am new to Java programming and looking for some help to learn it fast.Thanks
java - Hibernate
Java assert keyword example  What is the assert keyword? Can anyone explain it with the help of an example
How to integrate core java with Hibernate
with Hibernate How to integrate core java with Hibernate   xxxxxxxxx   Please visit the following link: Java with Hibernate The above link will provide you some tutorials of Core Java and Hibernate Integration.  
Java - Hibernate Interview Questions
Java  what is serialVersionUID and what does it play a role in hibernate
Java Struts - Hibernate
Java Struts  I am trying to do a completed project so that I can keep it in my resume. So can anyone please send me a completed project in struts and hibernate. I will recode it and practice it and attach the same in my resume
Java Compilation error. Hibernate code problem. Struts first example - Hibernate
Java Compilation error. Hibernate code problem. Struts first example  Java Compilation error. Hibernate code problem. Struts first example
Java - Hibernate Interview Questions
Java  how to write a sql join in hibernate query language?  ... and many more examples: Thanks RoseIndia Team
java compilation error - Hibernate
java compilation error  hi, i have made an registration page whosevalues should be inserted into database. i have used hibernate and eclipse it is working fine. but when i make a war file and deploy it using ant and then run
Java hibernate
Java hibernate      ... mapping tool for Java platform. Hibernate maps java classes to database tables... in the Java-based middle-tier. Hibernate allows transparent persistence
hibernate - Java Interview Questions
hibernate  compare to ejb3.0 what are the features are added in hibernate.  Hi friend, Fo solving the problem visit to :
hibernate  how to execute a sequence(in database) from Hibernate program or java program   Please visit the following link: Hibernate Tutorials
time Hibernate allows to map the Java objects to the relational database. Hibernate...why hibernate?  why hibernate?   Hibernate: -Hibernate... library. It solves object-relational impedance mismatch problems. Hibernate
What is Hibernate in Java?
What is Hibernate in Java? Hibernate is an ORM tools for Java. In this section... service for Java. If you will notices on the function of hibernate, it denotes... an stance for Java. In fact hibernate helps in solving the object-relational
java - Hibernate Interview Questions
java  what is pojo?  Hi friend, POJO, or Plain Old Java Object, is a normal Java object class (that is, not a JavaBean, EntityBean etc... interfaces of any of the Java frameworks. This term was coined by Martin Fowler
java compilation error - Hibernate
java compilation error  hi i am getting org.hibernate.exception.GenericJDBCException: Cannot open connection exception when runnig the code why this exception is coming
Java Hibernate
Java Hibernate      ...: Hibernate maps the java classes to database tables. Hibernate provides its own data... databases. Hibernate allows persistent transparency in case of Plain Old Java
What is Hibernate in Java?
What is Hibernate in Java? - Hibernate is ORM tool in Java. Are you beginner... Hibernate Framework in Java. The Hibernate in Java is framework used for developing... the complete information about Hibernate framework in Java. There are many different
Hibernate - Hibernate
Hibernate application development help  Hi, Can anyone help me in developing my first Java and Hibernate application
Hibernate Simple Program Using Java Application with Eclipse
Hibernate Simple Program Using Java Application with Eclipse  How to write Hibernate Simple Program Using Java Application with Eclipse? I wish to learn Hibernate using very popular Eclipse IDE. Share me the best tutorial link
hibernate  Is there any other way to call procedure in hibernate other than named query????? if we are using session object to get the connection then why hibernate we can directly call by using a simple java class??????? please
Java Spring Hibernate Struts Training java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based! org.hibernate.validator cannot be resolved error java.lang.ClassNotFoundException: org.hibernate.cache.TimestampsRegion java.lang.ClassNotFoundException: org.hibernate.cache.TimestampsRegion property hibernate.cache.region.factory_class is not given Hibernate example step by step in Eclipse with MySQL How to make Struts Hibernate and Spring Based Login/Registration Application? What is the use of Hibernate Native SQL? What is the use of Hibernate is used? How to integrate Struts and Hibernate in an application? How to create the program in Java for getting the forgotten Password Form? How to write create JPA Many-to-Many Relationship in Java? What is the DAO Layer in any application? What is Spring Struts Hibernate DAO Layer? How to write Example code for CRUD application in hibernate annotation? Where to update the Hibernate tools? Query conversion convert into hql how to convert sql into hql package org.hibernate.util does not exist C3P0 with Hibernate What do you understand by JPA? Hibernate Query Checker Jpa many to many relationship What do you understand by Hibernate in Java? What are criteria Queries in Hibernate? How to use Associations and Joins in Hibernate? Hibernate 4 Example with Eclipse How to learn Hibernate? What is Hibernate Framework? hibernate faq 15/03/14 hibernate 15/03/2014 Hibernate Criteria Wildcard how to add mulitiple projections this is a hibernate question this is a hibernate question this is a hibernate question how to writw connection pool coding Hibernate 4.3 and JPA 2.1 support How to create Hibernate session factory name? Can you provide me Hibernate configuration tutorial? I want to build sessionfactory in hibernate 4. Need help. I want to learn Hibernate tutorial quickly How to create sessionfactory in Hibernate 4.1.1? How to create sessionfactory in Hibernate 4.1? Hibernate 4.3.0.Final get session How to learn hibernate in 24 hours? hibernate 4.3.0 I want to know how can I create sessionfactory in hibernate 4 (latest version) standard serviceregistrybuilder example


Advertisement null