Hi All, I am very newbie to Hibernate.So today i was trying to do my first example in hibernate.I have created a mysql database . Then i got all the jar files required for hibernate.I am using eclipse IDE for that.I have configured in hibernate.cfg.xml , log4j.properties and hibernate.properties. When i was trying to run the program , I have succeeded to do hibernate mapping , then i am getting the following error::
19 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.3.2.GA 30 [main] INFO org.hibernate.cfg.Environment - loaded properties from resource hibernate.properties: {hibernate.connection.username=*******, hibernate.connection.password=*******, hibernate.connection.url=jdbc:mysql://localhost:3306/ipstorage?autoReconnect=true, hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.driver_class=com.mysql.jdbc.Driver} 34 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist 39 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling 130 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml 130 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml 267 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : com/java/hibernate/connection/beans/serverIps.hbm.xml 343 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: com.java.hibernate.connection.beans.serverIps -> server_ips 368 [main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: java:hibernate/SessionFactory 444 [main] INFO org.hibernate.connection.ConnectionProviderFactory - Initializing connection provider: org.hibernate.connection.C3P0ConnectionProvider 446 [main] INFO org.hibernate.connection.C3P0ConnectionProvider - C3P0 using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/ipstorage?autoReconnect=true 447 [main] INFO org.hibernate.connection.C3P0ConnectionProvider - Connection properties: {user=*******, password=******, autocommit=false} 447 [main] INFO org.hibernate.connection.C3P0ConnectionProvider - autocommit mode: false log4j:WARN No appenders could be found for logger (com.mchange.v2.log.MLog). log4j:WARN Please initialize the log4j system properly. 1240 [main] INFO org.hibernate.cfg.SettingsFactory - RDBMS: MySQL, version: 5.1.39-community 1240 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.6 ( Revision: ${svn.Revision} ) 1271 [main] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.MySQLDialect 1278 [main] INFO org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions) 1294 [main] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended) 1294 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled 1294 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled 1294 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15 1294 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled 1295 [main] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled 1295 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled 1295 [main] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto 1296 [main] INFO org.hibernate.cfg.SettingsFactory - Maximum outer join fetch depth: 2 1296 [main] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1 1296 [main] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled 1296 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled 1296 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled 1296 [main] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory 1298 [main] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory 1298 [main] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {} 1299 [main] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled 1299 [main] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled 1299 [main] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled 1304 [main] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge 1304 [main] INFO org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge - Cache provider: org.hibernate.cache.NoCacheProvider 1304 [main] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled 1304 [main] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled 1308 [main] INFO org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout 1309 [main] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled 1309 [main] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled 1309 [main] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo 1309 [main] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled 1368 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory 1524 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Factory name: java:hibernate/SessionFactory 1525 [main] INFO org.hibernate.util.NamingHelper - JNDI InitialContext properties:{} 1529 [main] WARN org.hibernate.impl.SessionFactoryObjectFactory - Could not bind factory to JNDI javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial at javax.naming.spi.NamingManager.getInitialContext(Unknown Source) at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source) at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source) at javax.naming.InitialContext.getNameParser(Unknown Source) at org.hibernate.util.NamingHelper.bind(NamingHelper.java:75) at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:113) at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:348) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341) at com.java.hibernate.connection.HibernateUtil.<clinit>(HibernateUtil.java:14) at c.java.hibernate.connection.mainText.main(mainText.java:14) Exception in thread "main" org.hibernate.SessionException: Session is closed! at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:72) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1021) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342) at $Proxy0.flush(Unknown Source) at com.java.hibernate.connection.hibernateImpl.getserverIps(hibernateImpl.java:44) at com.java.hibernate.connection.mainText.main(mainText.java:15)
My hibernate.cfg.xml file is:::
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="java:hibernate/SessionFactory">
<!-- Database connection settings --> <property name="hibernate.connection.datasource">java:/comp/env/jdbc/MySQLDB</property> <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <property name="hibernate.connection.username">***</property> <property name="hibernate.connection.password">***</property>
<!-- configuration pool via c3p0--> <property name="hibernate.c3p0.acquire_increment">5</property> <property name="hibernate.c3p0.idle_test_period">1800</property> <!-- seconds --> <property name="hibernate.c3p0.timeout">180</property> <!-- seconds --> <property name="hibernate.c3p0.max_size">100</property> <property name="hibernate.c3p0.max_statements">500</property> <property name="hibernate.c3p0.min_size">10</property>
<!-- property name="hibernate.c3p0.maxIdleTime">300</property -->
<property name="hibernate.c3p0.acquireRetryAttempts">30</property> <property name="hibernate.c3p0.testConnectionOnCheckout">true</property> <property name="hibernate.c3p0.preferredTestQuery">SELECT 1</property>
<!-- JDBC connection pool (use the built-in) <property name="connection.pool_size">1</property -->
<!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property>
<property name="connection.autocommit">false</property>
<!-- <property name="hibernate.transaction.factory_class"> org.hibernate.transaction.JTATransactionFactory </property> <property name="hibernate.transaction.manager_lookup_class"> org.hibernate.transaction.TransactionManagerLookup </property> -->
<!-- Drop and re-create the database schema on startup <property name="hbm2ddl.auto"></property> -->
<mapping resource="com/java/hibernate/connection/beans/serverIps.hbm.xml" />
</session-factory>
</hibernate-configuration>
my hibernate.properties is :
hibernate.connection.driver_class = com.mysql.jdbc.Driver hibernate.connection.url = jdbc:mysql://localhost:3306/ipstorage?autoReconnect=true hibernate.connection.username = ******** hibernate.connection.password = ********
My HibernateUtil.java file is::
package com.java.hibernate.connection;
import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static { try { // Create the SessionFactory from hibernate.cfg.xml sessionFactory = new Configuration().configure().buildSessionFactory(); } catch (HibernateException ex) { // Make sure you log the exception, as it might be swallowed System.err .println("Hibernate Error:::::Initial SessionFactory creation failed." + ex); ex.printStackTrace(); ex.getCause(); ex.getLocalizedMessage(); ex.getMessage(); ex.getClass(); throw new ExceptionInInitializerError(ex); } }
public static synchronized SessionFactory getSessionFactory() { return sessionFactory; } }
My hibernateImpl.java file is :::
package com.java.hibernate.connection;
import java.io.Serializable; import java.util.Iterator; import java.util.List;
import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction;
import com.java.hibernate.connection.beans.serverIps;
public class hibernateImpl implements Serializable {
private static final long serialVersionUID = 1L;
@SuppressWarnings("unchecked") public static void getserverIps(Session session) {
Transaction tx = null; try { tx = session.beginTransaction(); List serverips = session.createQuery( "select id from server_ips as server_id").list(); for (Iterator iter = serverips.iterator(); iter.hasNext();) { serverIps element = (serverIps) iter.next(); System.out.println(element.getId() + " ::: " + element.getServer_name()); } } catch (RuntimeException e) { if (tx != null && tx.isActive()) { try { // Second try catch as the rollback could fail as well tx.rollback(); } catch (HibernateException e1) { System.out.println("Error rolling back transaction"); } // throw again the first exception throw e; } } finally { session.flush(); session.close(); } } }
Any help will be very much appreciated. Thanks in Advance Regards, Faruk Ali Ahmed
|