Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Java Tutorials

Core Java
JSP
Servlet
JDBC
Hibernate
Struts 1
Struts 2
JSF
Spring
J2EE
J2ME
Web Services
Ajax
Dojo
MySQL
Latest Comments
thanks for your he
Eclipse ME
code
Developing Methodo
Sam Rabophala
  All Comments...
 

 

 
Struts Tutorials
*Stuts TOC
*Apache Struts Introduction
* Struts Controller
* Struts Action Class
* Struts ActionFrom Class
* Using Struts HTML Tags
*Struts Validator Framework    
*Client Side Address Validation    
*Struts Tiles
*tiles-defs.xml
*Struts DynaActionForm
*Struts File Upload
*Struts DataSource
*AGGREGATING ACTIONS
*Internationalization
Struts Resources
*Struts Books
*Struts Articles
*Struts Frameworks
*Struts IDE
*Struts Alternative
*Struts Links
*Struts Presentations
*Struts Projects
*Struts Software
*Struts Reference
*Struts Resources
*Other Struts Tutorial
Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Have tutorials?
Add your tutorial to our Java Resource and get tons of hits.

We offer free hosting for your tutorials. and exposure for thousands of readers. drop a mail
roseindia_net@yahoo.com
 
   

 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

Hibernate Runninge Xample

User Comments
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl
  Blink   Furl   Simpy   Y! MyWeb 

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

Current Comments

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

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Inserting Record
Exception in thread "main" org.hibernate.exception.GenericJDBCException: Cannot open connection
at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at org.hibernate.jdbc.AbstractBatcher.openConnection(AbstractBatcher.java:384)
at org.hibernate.jdbc.JDBCContext.connect(JDBCContext.java:141)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:88)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:73)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:66)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:130)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1809)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2171)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669)
at FirstExample.main(FirstExample.java:35)
Caused by: java.sql.SQLException: Unable to connect to any hosts due to exception: java.net.SocketException: Invalid argument: connect

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: Invalid argument: connect

STACKTRACE:

java.net.SocketException: Invalid argument: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:124)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:225)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1783)
at com.mysql.jdbc.Connection.<init>(Connection.java:450)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.jdbc.AbstractBatcher.openConnection(AbstractBatcher.java:381)
at org.hibernate.jdbc.JDBCContext.connect(JDBCContext.java:141)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:88)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:73)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:66)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:130)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1809)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2171)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669)
at FirstExample.main(FirstExample.java:35)


** END NESTED EXCEPTION **


at com.mysql.jdbc.Connection.createNewIO(Connection.java:1851)
at com.mysql.jdbc.Connection.<init>(Connection.java:450)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.jdbc.AbstractBatcher.openConnection(AbstractBatcher.java:381)
... 15 more
Done

Posted by sudheer on Monday, 12.22.08 @ 00:43am | #83027

while running this apllication i m getting like editor does't have main type.

Posted by sudheer on Monday, 12.15.08 @ 23:24pm | #82799

Hi (whoever wrote this example),

The code in FirstExample.java is incomplete. It will compile and execute fine but will now insert any records in the DB.

Change it as below:
Transaction tx = session.beginTransaction();
Contact contact = new Contact();
contact.setFirstName("Arun");
contact.setLastName("Jain");
contact.setEmail("akj_lh@yahoo.co.in");
session.save(contact);
tx.commit();


Regards
-Arun

Posted by Arun Jain on Monday, 12.8.08 @ 06:24am | #82509

Perfect tutorial..

Posted by ufuk on Thursday, 12.4.08 @ 14:45pm | #82405

When i was runnig the firstExample i hava a problem in NoClassDefFoundError

Posted by niranjan on Monday, 12.1.08 @ 06:42am | #82236

When the application and got output as shown in tutorial. but it did not insert any record in the database.
The reason is because the database operation need to be enclosed in a transaction and the it should be commited.

Here is the sample code:

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
org.hibernate.Transaction tx = session.beginTransaction();
Contact contact = new Contact();
contact.setId(1);
contact.setFirstName("Deepak");
contact.setLastName("kumar");
contact.setEmail("deepak_kumar@yahoo.com");
session.save(contact);
tx.commit();

Hope it will be useful for the some one struggling like me to run the application ;)

Posted by Kalyani on Sunday, 11.30.08 @ 00:53am | #82188

Hi Carl,

You get the following exceptions when you do not have the mysql-connector-java-3.0.16-ga-bin.jar and mysql-connector-java-3.1.6-bin.jar in the Java build path
------------------
Exception in thread "main" java.lang.NullPointerException
at roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:36)
--------------

If you are using eclipse, then Project -> properties - > java Build Path -> libraries tab -> ensure you have MySQL connectors mentioned above here...if not add using the 'Add jars' or 'Add External Jars' option

Posted by saikrishna on Wednesday, 10.15.08 @ 13:56pm | #81095

Thanks to Rose India, and thanks to Deepak, for implementing Hibernate, in such a simple fashion. This program has really worked for me. And helped me to enhance it, for complex application. Thanks again.

If any of you, get problem in inserting data in to contact table.

Create contact table in the following way.

CREATE TABLE `Contact` (

`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`firstName` VARCHAR( 50 ) NOT NULL ,
`lastName` VARCHAR( 250 ) NOT NULL ,
`email` VARCHAR( 100 ) NOT NULL ,
PRIMARY KEY ( `id` )
) TYPE = MYISAM ;

Posted by Pratap on Thursday, 10.9.08 @ 16:05pm | #80980

You have to create the table for contact for this to work

CREATE TABLE `Contact` (
`id` int(11) NOT NULL default '0',
`firstname` varchar(20) NOT NULL default '',
`lastname` varchar(20) NOT NULL default '',
`email` varchar(30) default NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM;

Posted by Amro on Wednesday, 09.17.08 @ 18:10pm | #80100

If you are getting nullpointer then there is problem like include all the jar files which are need for hibernate then it will work fine
required jar files for hibernate are:

hibernate3.jar
dom4j.jar
commons-logging.jar
commons-collections.jar
cglib.jar
concurrent.jar
derby-10.1.2.1.jar
jta1.0.1.jar

Posted by preeti on Monday, 09.15.08 @ 17:15pm | #79588

Reply to the questions asked in the forum(after the question).Make the tutorial as small as possible. I downloaded the tutorial and I am able to see many unwanted things which are not required to run a simple program(it may confuse). I too had the same problem which a user has already asked. If you would have answered to his question means i might not have asked the same question. My problem is
(I ran the application and got output as shown in tutorial. but it did not insert any record in database.)

Posted by thiagarajan on Saturday, 09.13.08 @ 14:12pm | #79102

For some reason, my application didn't insert any data into the table. What's wrong?

Posted by handreach on Sunday, 09.7.08 @ 04:06am | #77610

I am getting the following error when I try to run the FirstExample.java in eclipse.

Exception in thread "main" java.lang.NullPointerException
at roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:36)

Does anyone has a fix for this?

Posted by Carl on Wednesday, 09.3.08 @ 23:38pm | #77021

its really working ... good job..guys..

Posted by shyam on Friday, 08.15.08 @ 11:33am | #72930

while implementing first hibernate code i am getting NullPointerException in main.

Posted by Tejashree D. Bhanawala on Thursday, 08.14.08 @ 16:32pm | #72820

I ran the application and got output as shown in tutorial. but it did not insert any record in database.

I am using oracle 10g and my setting is
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
is it wrong ?

Posted by sandeep on Wednesday, 06.4.08 @ 16:18pm | #62081

Exception in thread "main" java.lang.ExceptionInInitializerError
at roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:16)
Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException) (Caused by org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException))
at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543)
at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
at net.sf.hibernate.cfg.Configuration.<clinit>(Configuration.java:95)
... 1 more
Caused by: org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException)
at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:397)
at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:529)
... 5 more
Caused by: java.lang.NullPointerException
at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:374)
... 6 more

Posted by Balamurugan on Wednesday, 06.4.08 @ 15:22pm | #62072

I followed the tutorial but can not run the example. I got the following exception when running the sample code:
Exception in thread "main" java.lang.NullPointerException
at roseindia.tutor.hibernate.FirstExample.main(FirstExample.java:39)

I tried to degub, and find out that after the below line (create a new SessionFactory object):
SessionFactory sf = new Configuration().configure().buildSessionFactory();
the programm went to session.flush() at the finally block.

Could you please tell me why? Thanks.

Posted by Lam on Tuesday, 05.13.08 @ 10:08am | #59660

how to create web page using Hibernate

Posted by prasad on Thursday, 05.8.08 @ 13:36pm | #58898

I got this when i tried to run it......
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Inserting Record
Done
Exception in thread "main" org.hibernate.exception.GenericJDBCException: Cannot open connection
at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at org.hibernate.jdbc.AbstractBatcher.openConnection(AbstractBatcher.java:384)
at org.hibernate.jdbc.JDBCContext.connect(JDBCContext.java:141)
........

not sure why though... i followed the tutorial perfectly...

Posted by B on Tuesday, 04.29.08 @ 07:27am | #58095

I am new to this hibernate/eclipse thing, but my goodness! this is so wonderful. Good work guys.

Posted by David on Wednesday, 03.26.08 @ 15:32pm | #54389

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

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.

Back to Tutorial

 

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  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.