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



Home Hibernate Running First Hibernate 3.0 Example

 
 

Share on Google+Share on Google+

Running First Hibernate 3.0 Example

Advertisement
In this section, you will learn about the hibernate Tutorial.

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 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.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 7, 2008

Related Tutorials

Discuss: Running First Hibernate 3.0 Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:17
Durgesh Kumar
July 8, 2012
FirstExample.java not working properly

I am using sql express as my db and the changes i have made in hibernate.cfg.xml are as follow <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property> <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=Indianbank2</property> <property name="hibernate.connection.username">DKG</property> <property name="hibernate.connection.password">abc</property> <property name="hibernate.connection.pool_size">10</property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.SQLServerDialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- Mapping files --> <mapping resource="contact.hbm.xml"/> </session-factory> </hibernate-configuration> i have added the jar file for sqljdbc4 in the build path now when i run the program it is running successfully untill it reaches System.out.println("1"); SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); System.out.println("2"); session =sessionFactory.openSession(); output i get on console is just 1 and not 2 that means my program is not being executed after that. no error message comes there just the warning message as usual i.e. log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. please solve my problem. i am using eclipse helios and microsoft sql server 2005.
maktoom
August 23, 2012
fail

hi, i copy/paste this codes on netbeans7.2 and after build and deploy i see this messages in run (DB is: mysql5.2): run: Aug 23, 2012 10:36:11 AM org.hibernate.cfg.Environment <clinit> INFO: Hibernate 3.2.5 Aug 23, 2012 10:36:11 AM org.hibernate.cfg.Environment <clinit> INFO: hibernate.properties not found Aug 23, 2012 10:36:11 AM org.hibernate.cfg.Environment buildBytecodeProvider INFO: Bytecode provider name : cglib Aug 23, 2012 10:36:11 AM org.hibernate.cfg.Environment <clinit> INFO: using JDK 1.4 java.sql.Timestamp handling Aug 23, 2012 10:36:11 AM org.hibernate.cfg.Configuration configure INFO: configuring from resource: /hibernate.cfg.xml Aug 23, 2012 10:36:11 AM org.hibernate.cfg.Configuration getConfigurationInputStream INFO: Configuration resource: /hibernate.cfg.xml Aug 23, 2012 10:36:11 AM org.hibernate.cfg.Configuration addResource INFO: Reading mappings from resource : contact.hbm.xml Aug 23, 2012 10:36:12 AM org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues INFO: Mapping class: roseindia.tutorial.hibernate.Contact -> CONTACT Aug 23, 2012 10:36:12 AM org.hibernate.cfg.Configuration doConfigure INFO: Configured SessionFactory: null Aug 23, 2012 10:36:12 AM org.hibernate.connection.DriverManagerConnectionProvider configure INFO: Using Hibernate built-in connection pool (not for production use!) Aug 23, 2012 10:36:12 AM org.hibernate.connection.DriverManagerConnectionProvider configure INFO: Hibernate connection pool size: 10 Aug 23, 2012 10:36:12 AM org.hibernate.connection.DriverManagerConnectionProvider configure INFO: autocommit mode: false Aug 23, 2012 10:36:12 AM org.hibernate.connection.DriverManagerConnectionProvider configure INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/hibernatetutorial Aug 23, 2012 10:36:12 AM org.hibernate.connection.DriverManagerConnectionProvider configure INFO: connection properties: {user=root, password=****} Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: RDBMS: MySQL, version: 5.5.20 Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.18 ( Revision: tonci.grgin@oracle.com-20110930151701-jfj14ddfq48ifkfq ) Aug 23, 2012 10:36:12 AM org.hibernate.dialect.Dialect <init> INFO: Using dialect: org.hibernate.dialect.MySQLDialect Aug 23, 2012 10:36:12 AM org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory INFO: Using default transaction strategy (direct JDBC transactions) Aug 23, 2012 10:36:12 AM org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended) Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Automatic flush during beforeCompletion(): disabled Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Automatic session close at end of transaction: disabled Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: JDBC batch size: 15 Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: JDBC batch updates for versioned data: disabled Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Scrollable result sets: enabled Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: JDBC3 getGeneratedKeys(): enabled Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Connection release mode: auto Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Maximum outer join fetch depth: 2 Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Default batch fetch size: 1 Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Generate SQL with comments: disabled Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Order SQL updates by primary key: disabled Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Order SQL inserts for batching: disabled Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory Aug 23, 2012 10:36:12 AM org.hibernate.hql.ast.ASTQueryTranslatorFactory <init> INFO: Using ASTQueryTranslatorFactory Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Query language substitutions: {} Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: JPA-QL strict compliance: disabled Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Second-level cache: enabled Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Query cache: disabled Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory createCacheProvider INFO: Cache provider: org.hibernate.cache.NoCacheProvider Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Optimize cache for minimal puts: disabled Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Structured second-level cache entries: disabled Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Echoing all SQL to stdout Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Statistics: disabled Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Deleted entity synthetic identifier rollback: disabled Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Default entity-mode: pojo Aug 23, 2012 10:36:12 AM org.hibernate.cfg.SettingsFactory buildSettings INFO: Named query checking : enabled Aug 23, 2012 10:36:12 AM org.hibernate.impl.SessionFactoryImpl <init> INFO: building session factory Aug 23, 2012 10:36:13 AM org.hibernate.impl.SessionFactoryObjectFactory addInstance INFO: Not binding factory to JNDI, no JNDI name configured Aug 23, 2012 10:36:13 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: Running hbm2ddl schema update Aug 23, 2012 10:36:13 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: fetching database metadata Aug 23, 2012 10:36:13 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: updating schema Aug 23, 2012 10:36:13 AM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: table found: hibernatetutorial.contact Aug 23, 2012 10:36:13 AM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: columns: [id, email, lastname, firstname] Aug 23, 2012 10:36:13 AM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: foreign keys: [] Aug 23, 2012 10:36:13 AM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: indexes: [primary] Aug 23, 2012 10:36:13 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: schema update complete Inserting Record Done Hibernate: insert into CONTACT (FIRSTNAME, LASTNAME, EMAIL, ID) values (?, ?, ?, ?)
Sumit Deshinge
December 27, 2012
No Output

Record is not getting insr=erted into the tables
ravi
September 10, 2012
doubt

i want hibernate3.jar files source code,because i want to know what will be there in session factory,session object,configure(),....please help me where it avilable.
ravi
September 10, 2012
Doubt

i want source code for hibernate3.jar file,because i want to know what will be there in session factory,session object,configure().Please help me where it available
Your Name
January 1, 2013
YourEmail

YourEmail
Ibai
June 13, 2013
0 rows inserted SOLUTION

try{ SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); session =sessionFactory.openSession(); org.hibernate.Transaction tx = session.beginTransaction(); System.out.println("Inserting Record"); Contact contact = new Contact(); contact.setId(6); contact.setFirstName("Deepak"); contact.setLastName("Kumar"); contact.setEmail("deepak_38@yahoo.com"); session.save(contact); System.out.println("Done"); tx.commit(); } catch(Exception e){ . . . } finally { . . . }
Deandra
June 15, 2013
RQMedNlsheeRN

That's a well-thought-out answer to a challenging qesution
Diwakar
March 18, 2014
Need Help - First Hibernate Example

I am getting the below error while trying to run the example . Please help Could not parse configuration: /hibernate.cfg.xml Exception in thread "main" java.lang.NullPointerException at roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:38)
DMCA.com