0

I have several .hbm.xml mapping files set up in Spring, and I can tell from the logs that Spring sees them and does something with them, but no tables are created in the DB.

Bean setup:

 <beans:bean id="dataSourceEmbedded" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <beans:property name="driverClassName" value="org.h2.Driver" />
    <beans:property name="url" value="jdbc:h2:~/myDbName" />
    <beans:property name="username" value="sa" />
    <beans:property name="password" value="" />
</beans:bean>

<beans:bean id="sessionFactory" 
      class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
      p:dataSource-ref="dataSourceEmbedded">
    <beans:property name="mappingLocations">
        <beans:list>
            <beans:value>classpath:path/to/schemas/Page.hbm.xml</beans:value>                                    
            <beans:value>classpath:path/to/schemas/Navigation.hbm.xml</beans:value>
        </beans:list>
    </beans:property>
    <beans:property name="hibernateProperties">
        <beans:value>
            hibernate.show_sql=true
            hibernate.dialect=${hibernate.dialect}
            hibernate.hbmToDdlAuto=create
        </beans:value>
    </beans:property>
</beans:bean>        

I know that the path to the Page and Navigation mapping files is correct - an error is thrown if it's not, and I can see the fields being mapped during the tomcat startup:

DEBUG: org.hibernate.cfg.Configuration - Processing hbm.xml files
INFO : org.hibernate.cfg.HbmBinder - Mapping class: Page -> pages
DEBUG: org.hibernate.cfg.HbmBinder - Mapped property: Page ID -> pageid
DEBUG: org.hibernate.cfg.HbmBinder - Mapped property: title -> title

Similarly, the database is clearly accessed / locked while tomcat is loading, so something is happening in there! But once it's loaded and I try to query it or inspect it, no tables exist.

Question: Why aren't my tables being created / what have I done wrong?

2 Answers2

2

The property you used is wrong, it should be 'hibernate.hbm2ddl.auto' instead of 'hibernate.hbmToDdlAuto'... However instead of that you can also set the schemaUpdate property of the LocalSessionFactoryBean.

<beans:bean id="sessionFactory" 
      class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
      p:dataSource-ref="dataSourceEmbedded">
    <beans:property name="mappingLocations">
        <beans:list>
            <beans:value>classpath:path/to/schemas/Page.hbm.xml</beans:value>                                    
            <beans:value>classpath:path/to/schemas/Navigation.hbm.xml</beans:value>
        </beans:list>
    </beans:property>
    <beans:property name="schemaUpdate" value="true" />
    <beans:property name="hibernateProperties">
        <beans:value>
            hibernate.show_sql=true
            hibernate.dialect=${hibernate.dialect}
        </beans:value>
    </beans:property>
</beans:bean>     

Related answer/info: Hibernate hbm2ddl.auto possible values and what they do?

Community
  • 1
  • 1
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
1

Correct property name is hibernate.hbm2ddl.auto

Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69