Quick post – hibernate.cfg.xml with Hibernate 4.0.0

March 21, 2012 at 11:55 am 2 comments

Hello, everyone! Yesterday I spent many hours trying to make Hibernate 4.0 (hibernate-core-4.0.0) work with a hibernate.cfg.xml file. I just could not obtain a Session Factory from a file similar to this one:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
<property name="connection.url">jdbc:mysql://localhost/noob</property>

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- DB schema will be updated if needed -->
<property name="hbm2ddl.auto">create-drop</property>
<property name="show_sql">false</property>
<property name="format_sql">false</property>


Besides, I was trying to obtain a SessionFactory with a code like this:

public class HibernateUtil {

private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

private static SessionFactory configureSessionFactory() throws HibernateException {
Configuration configuration = new Configuration();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;

public static SessionFactory getSessionFactory() {
return configureSessionFactory();


And no, it was not working. So I made some changes:
Into my hibernate.cfg.properties, I did something like this:

<?xml version='1.0' encoding='utf-8'?>

<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/noob</property>
<property name="hibernate.connection.username">root</property>

<property name="hibernate.connection.password"></property>

<!-- JDBC connection pool (use the built-in) -->
<property name="hibernate.connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="hibernate.current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider

<!-- Echo all executed SQL to stdout -->
<property name="hibernate.show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="hibernate.hbm2ddl.auto">update</property>


Notice the “hibernate.” in front of the properties. It was very very helpful, without it things did not work.

Also, for my SessionFactory, I did a code like this:

ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().configure().buildServiceRegistry();
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
SessionFactory factory = metadataSources.buildMetadata().buildSessionFactory();

And win!!! Things started working!


Entry filed under: Hibernate, JBoss.

EJB Lookup w/ JNDI on Jboss AS 7

2 Comments Add your own

  • 1. Vijay  |  November 21, 2012 at 4:23 am

    Cool n Thx.

  • 2. Tomas  |  January 4, 2013 at 7:57 pm

    Thank you man.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


March 2012
« Dec    

Most Recent Posts

%d bloggers like this: