Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Hibernate
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Running First Hibernate 3.0 Example

                         

Hibernate is free open source software it can be download from http://www.hibernate.org/6.html. Visit the site and download Hibernate 3.0. You can download the Hibernate and install it yourself. But I have provided very thing in one zip file. Download the example code and library from here and extract the content in your favorite directory say "C:\hibernateexample". Download file contains the Eclipse project. To run the example you should have the Eclipse IDE on your machine. Start the Eclipse project and select Java Project as shown below.

Click on "Next" button. In the new screen, enter "hibernateexample" as project name and browse the extracted directory "C:\hibernateexample". 

Click on "Next" button. In the next screen leave the output folder as default "hibernateexample/bin" .

Click on the "Finish" button.

Now Open the FirstExample.java in the editor as show below.

 

Copy  contact.hbm.xml, and hibernate.cfg.xml in the bin directory of the project using windows explorer. To run the example select Run-> Run As -> Java Application from the menu bar as shown below.

This will run the Hibernate example program in Eclipse following output will displayed on the Eclipse Console.

In this section I showed you how to run the our first Hibernate 3.0 example.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

106 comments so far (post your own) View All Comments Latest 10 Comments:

Hi kruti,

Hope ur problem is solved, in case if its not, just add the required libraries in ur buildpath.To run hibernate pgm properly, u need to add the required libraries in the projects buildpath.

Posted by shruthi on Tuesday, 03.18.08 @ 19:50pm | #53207

when i am trying to run hibernate factory class in debug mode then on buildsessionFactory line i am getting error that "The hibernate3.jar source not found"

pls tell me how to solve this

Posted by kruti on Friday, 03.7.08 @ 12:13pm | #51815

Hi Faaud,

You can resolve your problem below problem
------------------------------------------
Exception in thread "main" java.lang.NoClassDefFoundError: net.sf.cglib.core.DebuggingClassWriter$1
------------------------------------------

by doing this:

You have add cglib-2.1.3.jar file into your class file.

OR

If you are working with eclipse 3.0 or 3.1 then this jar file should be added to user library.

OR

go to PROJECT - > PROPERTIES -> JAVA BUILD PATH -> LIBRARIES -> Add External Jars ->
define the path for this jar file.

Now i think you woun't get any error.

Posted by Subnesh on Monday, 03.3.08 @ 14:19pm | #51131

When I run code of sample example at line :

sessionFactory = new Configuration().configure().buildSessionFactory();

I get following error:

Exception in thread "main" java.lang.NoClassDefFoundError: net.sf.cglib.core.DebuggingClassWriter$1
at java.lang.Class.initializeClass(libgcj.so.8rh)
at net.sf.cglib.core.DebuggingClassWriter.toByteArray(DebuggingClassWriter.java:66)
at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:26)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:215)
at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:117)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)
at org.hibernate.impl.SessionFactoryImpl.<clinit>(SessionFactoryImpl.java:308)
at java.lang.Class.initializeClass(libgcj.so.8rh)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1043)
at FirstExample.main(FirstExample.java:21)
Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.util.TraceClassVisitor not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:/home/nis/batch2007/FAnwar/workspace/Test/New/bin/,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/ant-1.6.2.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/ant-antlr-1.6.2.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/ant-junit-1.6.2.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/ant-launcher-1.6.2.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/antlr-2.7.4.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/ant-swing-1.6.2.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/c3p0-0.8.5.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/cglib-full-2.0.2.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/cleanimports.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/commons-collections-2.1.1.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/commons-logging-1.0.4.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/concurrent-1.3.2.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/connector.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/dom4j-1.5.2.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/ehcache-1.1.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/hibernate3.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/jaas.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/jacc-1_0-fr.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/jaxen-1.1-beta-4.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/jboss-cache.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/jboss-common.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/jboss-jmx.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/jboss-remoting.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/jboss-system.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/jdbc2_0-stdext.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/jgroups-2.2.7.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/jta.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/junit-3.8.1.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/log4j-1.2.9.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/mysql-connector-java-3.0.16-ga-bin.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/mysql-connector-java-3.1.6-bin.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/oscache-2.1.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/proxool-0.8.3.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/swarmcache-1.0rc2.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/versioncheck.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/xerces-2.6.2.jar,file:/home/nis/batch2007/FAnwar/workspace/hibernate/lib/xml-apis.jar], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
at java.net.URLClassLoader.findClass(libgcj.so.8rh)
at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.8rh)
at java.lang.ClassLoader.loadClass(libgcj.so.8rh)
at java.lang.ClassLoader.loadClass(libgcj.so.8rh)
at java.lang.Class.forName(libgcj.so.8rh)
at java.lang.Class.initializeClass(libgcj.so.8rh)
...11 more

Posted by Faaud on Thursday, 02.28.08 @ 19:07pm | #50586

when i tried the example program with transaction autocommited,i am still getting message like
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Inserting Record
Hibernate: insert into CONTACT (FIRSTNAME, LASTNAME, EMAIL, ID) values (?, ?, ?, ?)
Could not execute JDBC batch update

Posted by panbil on Thursday, 02.28.08 @ 14:59pm | #50403

At the time of working with this example:
- table gets created in database
- but no record is inserted

The given below is the solution for the Porblem:

In your FirstExample.java, the transaction should be started explicitly and after saveing your contact details transaction should be commited explicitly.


write below lines before the doing anything with your contact class.
-----------------------------------------------
tx = session.beginTransaction();

//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Creating New Contact detail.");
Contact contact = new Contact();
System.out.println("providing contact details");
contact.setId(6); // Primary key has to be unique for every record inserted!
contact.setFirstName("subnesh");
contact.setLastName("sharma");
contact.setEmail("subnesh@gmail.com");
session.save(contact);
tx.commit();
System.out.println("Contact saved into database successfully");
------------------------------------------------


or

rather then making changes in your FirstExample.java (use same code as privided in this tutorial), you can add new property to hibernate.cfg.xml file with below code:

<property name="hibernate.connection.autocommit"> true </property>
------------------------------------


Posted by subnesh on Wednesday, 02.27.08 @ 11:44am | #50282

Its really nice example to the things in a repid way. Understandable and more focus is on "how to do mappings".
Good work done

As some other developers suggested to do some changes in the code, i made them and then got the data in database.

Thanks again buddy :)

Posted by Subnesh on Wednesday, 02.27.08 @ 11:19am | #50277

hi i run that program. the output in the console came as shown in the tutorial. but when i cheak the database nothing is inserted in that. did any one cheaked it. please give me suggestion. because i am new to hibernate and to java

Posted by sandeep on Tuesday, 02.26.08 @ 06:35am | #50098

Thank you. It is good to start with. I started with this example, faced some problems, but finally I got through.

Posted by Pradeep Kumar on Thursday, 02.14.08 @ 17:02pm | #48265

it is really nice tutorial for bigners;

Posted by vishvadeep on Wednesday, 02.13.08 @ 21:08pm | #48167

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.