You can use following code to build session factory in HibernateUtil class.
Code:
Configuration c = new Configuration();
c.addJar( new File( "/my-mappings.jar" ) );
Properties p = new Properties();
p.put( "hibernate.connection.driver_class", "oracle.jdbc.OracleDriver" );
p.put( "hibernate.connection.url", "YOUR_DB_URL" );
p.put( "hibernate.connection.username", "USER" );
p.put( "hibernate.connection.password", "PASSWORD" );
p.put( "hibernate.transaction.auto_close_session", "false" );
p.put( "hibernate.connection.pool_size", "1" );
p.put( "hibernate.dialect", "org.hibernate.dialect.Oracle9Dialect" );
p.put( "hibernate.current_session_context_class", "thread" );
p.put( "hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider" );
p.put( "hibernate.show_sql", "true" );
p.put( "hibernate.format_sql", "true" );
p.put( "hibernate.use_sql_comments", "false" );
p.put( "hibernate.hibernate.type", "true" );
c.addProperties( p );
sessionFactory = c.buildSessionFactory();
Instead of adding jar file with mappings as in the second line of the code, you can also use
Code:
c.addResource( "User.hbm.xml" );//correct path is necessary
If you just use the code
Code:
Configuration c = new Configuration();
SessionFactory sfac = c.configure().buildSessionFactory();
hibernate expects to find hibernate.cfg.xml file in the root of the classpath.