Archive for March 21, 2012
Quick post – hibernate.cfg.xml with Hibernate 4.0.0
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" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <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> </session-factory> </hibernate-configuration>
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(); configuration.configure(); 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'?> <hibernate-configuration xmlns="http://www.hibernate.org/xsd/hibernate-configuration" > <session-factory> <!-- 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 </property> <!-- 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> </session-factory> </hibernate-configuration>
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!