Home Hibernate Running First Hibernate 3.0 Example
Questions:Ask|Latest


 
 

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.

Advertisements

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

Ask Questions?    Discuss: Running First Hibernate 3.0 Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Sivaranjani
September 16, 2011
First Hibernate Example

I have run the Hibernate example successfully. But I could not see the data in the data base. The following is displayed on My console. Can anybody help me out. - Hibernate 3.0 beta 2 - hibernate.properties not found - using CGLIB reflection optimizer - using JDK 1.4 java.sql.Timestamp handling - configuring from resource: /hibernate.cfg.xml - Configuration resource: /hibernate.cfg.xml - Mapping resource: contact.hbm.xml - Mapping class: com.fourcs.clm.sh.utilities.Source -> WC_SRC - Configured SessionFactory: null - processing extends queue - processing collection mappings - processing association property references - processing foreign key constraints - Using dialect: org.hibernate.dialect.SQLServerDialect - Generate SQL with comments: disabled - Order SQL updates by primary key: disabled - Query translator: org.hibernate.hql.classic.ClassicQueryTranslatorFactory - Query language substitutions: {} - Using Hibernate built-in connection pool (not for production use!) - Hibernate connection pool size: 10 - autocommit mode: false - using driver: com.microsoft.sqlserver.jdbc.SQLServerDriver at URL: jdbc:sqlserver://10.1.113.17:1433;DatabaseName=SHLMHDDB - connection properties: {user=wcowner, password=****} - Scrollable result sets: enabled - JDBC3 getGeneratedKeys(): enabled - Using default transaction strategy (direct JDBC transactions) - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended) - Automatic flush during beforeCompletion(): disabled - Automatic session close at end of transaction: disabled - Cache provider: org.hibernate.cache.EhCacheProvider - Second-level cache: enabled - Optimize cache for minimal puts: disabled - Structured second-level cache entries: enabled - Query cache: disabled - Echoing all SQL to stdout - Statistics: disabled - Deleted entity synthetic identifier rollback: disabled - building session factory - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/D:/Development/Landmark/CLMEAR/lib/ehcache-1.1.jar!/ehcache-failsafe.xml - Not binding factory to JNDI, no JNDI name configured - Using dialect: org.hibernate.dialect.SQLServerDialect - Using Hibernate built-in connection pool (not for production use!) - Hibernate connection pool size: 10 - autocommit mode: false - using driver: com.microsoft.sqlserver.jdbc.SQLServerDriver at URL: jdbc:sqlserver://10.1.113.17:1433;DatabaseName=SHLMHDDB - connection properties: {user=wcowner, password=****} - Running hbm2ddl schema update - fetching database metadata - updating schema - processing extends queue - processing collection mappings - processing association property references - processing foreign key constraints - table found: SHLMHDDB.showner.WC_SRC - columns: [eff_end_dt, created_by_appln, updated_dt, src_cd, brand, prnt_src_id, updated_by_appln, src_crncy_cd, src_id, created_dt, src_lang_cd, updated_by_user, provider_id, src_typ, is_active, created_by_user, eff_bgn_dt, src_status] - foreign keys: [] - indexes: [uksr0001, pksr0001, ix_wc_src_1] - schema update complete - cleaning up connection pool: jdbc:sqlserver://10.1.113.17:1433;DatabaseName=SHLMHDDB - Checking 0 named queries Inserting Record Done Finally Hibernate: insert into SHOWNER.WC_SRC (PROVIDER_ID, SRC_CD, SRC_TYP, IS_ACTIVE, EFF_END_DT, CREATED_BY_APPLN, UPDATED_DT, BRAND, PRNT_SRC_ID, UPDATED_BY_APPLN, SRC_CRNCY_CD, CREATED_DT, SRC_LANG_CD, UPDATED_BY_USER, CREATED_BY_USER, EFF_BGN_DT, SRC_STATUS, SRC_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
hari
October 20, 2011
hi

show more
Nagaraju
October 26, 2011
Some doubts on hibernate first example

Hello sir, In hibernate first example CONTACT table was created while running in myeclipse but the day doesn't inserted into the table. What about Id value, firstname, lastname and email.These values are passing from application but those are not persisted into the database table. What happen please clarify my doubt.. thank you sir
Claudio
November 18, 2011
FIrstExample.java does not insert into table

I have followed the stepes and got the corect output on the Eclipse Console but when I check MySQL table it has 0 rows. The only thing this example does to my database is to creata a table CONTACT but with 0 rows.
Jeevan
December 9, 2011
not able to insert data

Hi, I am not able to insert data. it is giving a null pointer exception during configuration.configure line. Give me some more details such as where to place xml files.
cyril
December 28, 2011
Data is not inserted in the table using Hibernate

Hi, I have downloaded your hibernate example when I try to run "FirstExample.java" there is no errors and contact table created in my MYSQL database but the data in that table is not inserted. Can I find any other solution?? thanks Cyril.
bharath
May 1, 2012
not getting output :(

I am getting null exception when i execute it.. Please let me know if there is any solution for this.. I am very new to hibernate so help me.
Perumal
June 27, 2012
Error during Running the Hibernate application.

HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException Jun 27, 2012 4:36:03 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService am getting this error.... its not at all creating a table and not even performing any operation on table.. please help to resolve this problem
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