These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]

Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: The content of element type "session-factory" must match...
PostPosted: Sat Feb 25, 2012 12:44 pm 

Joined: Sat Feb 25, 2012 12:34 pm
Posts: 1
I'm trying to follow the example here:

Its out of date (e.g. it uses a deprecated method), so I'm trying to get it to work with hibernate 4.0.1

I get this error when running FirstExample:

ERROR: HHH000197: Error parsing XML: /hibernate.cfg.xml(20) The content of element type "session-factory" must match "(property*,mapping*,(class-cache|collection-cache)*,event*,listener*)".
invalid configuration
Exception in thread "main" java.lang.NullPointerException
at roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:61)

Can anyone offer assistance?

package roseindia.tutorial.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

* @author Deepak Kumar
* http://www.roseindia.net
* Hibernate example to inset data into Contact table
public class FirstExample {

   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;
    * @param args
   public static void main(String[] args) {
      Session session = null;

         // This step will read hibernate.cfg.xml and prepare hibernate for use
         SessionFactory sessionFactory = configureSessionFactory();
         session =  sessionFactory.openSession();
         //Create new instance of Contact and set values in it by reading them from form object
         System.out.println("Inserting Record");
         Contact contact = new Contact();
      catch(Exception e) {
      finally {
         // Actual contact insertion will happen at this step




<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"

  <property name="hibernate.connection.driver_class">
  <property name="hibernate.connection.url">
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password"></property>
  <property name="hibernate.connection.pool_size">10</property>
  <property name="show_sql">true</property>
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.hbm2ddl.auto">update</property>
  <!-- Mapping files -->
  <mapping resource="contact.hbm.xml"/>

I guess the xml file is not valid. I tried adding:


  <event type="load">
     <listener class="org.hibernate.event.def.DefaultLoadEventListener"/>
  <!-- Register the listeners -->
  <listener type="post-update" class="org.hibernate.event.def.DefaultLoadEventListener"/>

but still get the same error.

 Post subject: Re: The content of element type "session-factory" must match...
PostPosted: Mon Feb 27, 2012 4:31 am 

Joined: Fri Feb 17, 2012 8:20 am
Posts: 18
Try these hibernate tutorials...

Hibernate Tutorials from start to End

Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.