Announcement Announcement Module
Collapse
No announcement yet.
hibernate.hbm2ddl.auto not honoured? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • hibernate.hbm2ddl.auto not honoured?

    Hi,

    I've used hibernate for a while, but am new to integrating it into spring beans. I have a context XML file (as below). The hibernate properties specified are the same as I use in my hibernate unit tests. When I run the hibernate unit tests standalone, the database is auto-created on startup. However, when I use spring to get the session factory, hibernate is not auto creating the db.

    I know I'm probably doing something wrong, but I can't figure out what it is

    Thanks,
    John.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "file://localhost/C:/development/spring-framework-2.1-m1/dist/resources/spring-beans-2.0.dtd">
    
    <beans>
    
    	<bean id="h2DataSource"
    		class="com.foo.util.persistence.DataSource">
    		<property name="driverClassName">
    			<value>org.h2.Driver</value>
    		</property>
    		<property name="url">
    			<value>jdbc:h2:mem:InMemoryDatabase</value>
    		</property>
    	</bean>
    
    	<!-- Database Property -->
    	<bean id="h2HibernateProperties"
    		class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    		<property name="properties">
    			<props>
    				<prop key="hibernate.connection.username">sa</prop>
    				<prop key="hibernate.connection.password"></prop>
    				<prop key="hibernate.dialect">
    					org.hibernate.dialect.H2Dialect</prop>
    				<prop key="hibernate.show_sql">true</prop>
    				<prop key="hibernate.current_session_context_class">thread</prop>
    				<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
    				<prop key="hibernate.hbm2ddl.auto">create</prop>
    			</props>
    		</property>
    	</bean>
    
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource">
    			<ref local="h2DataSource"/>
    		</property>
    		<property name="hibernateProperties">
    			<ref bean="h2HibernateProperties"/>
    		</property>
    		<property name="mappingResources">
    			<list>
    				<value>com/foo/bar/User.hbm.xml</value>
    			</list>
    		</property>
    
    	</bean>
    </beans>
    Last edited by johnnyleitrim; Jul 18th, 2007, 04:22 AM. Reason: Update

  • #2
    These two should be defined in the DataSource, not in the Hibernate config:

    <prop key="hibernate.connection.username">sa</prop>
    <prop key="hibernate.connection.password"></prop>

    These two should be handled by Spring, you don't need/want to change defaults usually:

    <prop key="hibernate.current_session_context_class">thre ad</prop>
    <prop key="hibernate.transaction.factory_class">org.hibe rnate.transaction.JDBCTransactionFactory</prop>

    Comment


    • #3
      Problem solved

      I found out what the problem was. It appears that the H2 in memory database starts and stops each time a new connection is opened and closed. The spring session factory creator was opening a connection, creating all the necessary tables, and then closing the connection. The next time a new connection was opened, the in memory database was started from scratch again, use no tables created.

      I found that using the standard embedded process H2 database (without the 'mem' bit in the URL) worked fine, but it does leave around files though

      Comment

      Working...
      X