"session-factory" must match "(property*,mapping*,(class-cache|collection-cache)*,event*,listener*)"

4.8k views Asked by At

I can't find what's wrong with my hibernate.cfg.xml file.

<?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>
- <!--  Database connection settings 
  --> 
  <property name="connection.driver_class">org.h2.Driver</property> 
  <property name="connection.url">jdbc:h2://localhost:1527/hibernatedb</property> 
  <property name="connection.username">h2</property> 
  <property name="connection.password" /> 
- <!--  JDBC connection pool (use the built-in) 
  --> 
  <property name="connection.pool_size">1</property> 

- <!--  Disable the second-level cache  
  --> 
  <property name="cache.provider_class">org.hibernate.cache.internal.CollectionCacheInvalidator
  </property> 
- <!--  Echo all executed SQL to stdout 
  --> 
  <property name="show_sql">true</property> 
- <!--  Drop and re-create the database schema on startup 
  --> 
  <property name="hbm2ddl.auto">create</property> 

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

- <!--  Names the annotated entity class 
  --> 
  <mapping class="firsthibernateproj.UserDetails" />

  </session-factory>
  </hibernate-configuration>

Here is the class that I use:

    public class HibernateTest {
    private static Object configuration;


    public static void main(String[] args) {
        UserDetails user = new UserDetails();
        user.setUserId(1);
        user.setUserName("First User");

        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
        applySettings(configuration.getProperties());
        SessionFactory factory = configuration.buildSessionFactory(builder.build()); 


        Session session = factory.openSession();
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();

    }

}

xml file was checked, there no grammar mistakes.

Any ideas how can I solve this problem?

6

There are 6 answers

2
Predrag Maric On

Your file copied to my project also reports the same error, but the error goes away when those - characters before every comment are removed. Did you copy the content from some app that formats the xml? e.g. Internet Explorer does that

EDIT :)

Try this

<?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>
  <!--  Database connection settings 
  --> 
  <property name="connection.driver_class">org.h2.Driver</property> 
  <property name="connection.url">jdbc:h2://localhost:1527/hibernatedb</property> 
  <property name="connection.username">h2</property> 
  <property name="connection.password" /> 
  <!--  JDBC connection pool (use the built-in) 
  --> 
  <property name="connection.pool_size">1</property> 

  <!--  Disable the second-level cache  
  --> 
  <property name="cache.provider_class">org.hibernate.cache.internal.CollectionCacheInvalidator
  </property> 
  <!--  Echo all executed SQL to stdout 
  --> 
  <property name="show_sql">true</property> 
  <!--  Drop and re-create the database schema on startup 
  --> 
  <property name="hbm2ddl.auto">create</property> 

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

  <!--  Names the annotated entity class 
  --> 
  <mapping class="firsthibernateproj.UserDetails" />

  </session-factory>
  </hibernate-configuration>
0
Ummer Iqbal On

I had passed from same problem.But i have been solved this by just arranging all xml tag in a sequence ..

0
Arun Raaj On

I too had faced the same error once. I found out that I wrote a description line in hibernate.cfg.xml and i did not 'comment' that down.

it seems u also made the same mistake. I see an additional hyphen before every comment in your conf file.

"

- <!--  JDBC connection pool (use the built-in) 
  --> 

"

it should be "

<!--  JDBC connection pool (use the built-in) 
  -->

"

that's the only mistake i guess

0
phradr On

This problem occures if you copy sourcecode from the internet with a wrong (i.e. not matching the encoding of the target-file) encoding. If your overwrite EVERY single whitespace, line return, every character - EVERYTHING by hand, your file will work.

Alternatively you can also paste the code into an "stupid" editor, mark all and copy again. That could do the trick, too (not tested).

0
velocity On

I had the same eror. After searching and redeploying for about an hour. It turned out that i mistakinly typed a 'Q' somewhere in the configuration file and could not see it. So check your file closely, you must have misspelled something or typed some characters.

0
Buddhima Udaranga On

This problem occurs when you don't make the configuration file in order first list property then mapping and finally cache.