Hibernate 4.1.2.FINAL属性hbm2ddl.import_files似乎不起作用

时间:2012-05-01 01:09:50

标签: sql spring hibernate jboss

嗨,我有一个问题尊重一个hbm2ddl.import_files,似乎不工作,似乎没有出现在日志中。 这是我的配置:

<property name="hibernateProperties">
        <value>
            hibernate.dialect=${hibernate.dialect}
            hibernate.default_schema=${hibernate.default_schema}
            hibernate.jdbc.batch_size=${hibernate.jdbc.batch_size}
            hibernate.show_sql=${hibernate.show_sql}
            hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto}
            hibernate.id.new_generator_mappings=${hibernate.id.new_generator_mappings}
            hibernate.hbm2ddl.import_files=${hibernate.hbm2ddl.import_files}
            <!-- Auto Generated Schemas and tables not good for production
            hibernate.hbm2ddl.auto=update-->
         </value>
    </property>

hibernate.hbm2ddl.import_files = / import.sql,文件为:

insert into DEPARTAMENTO (NOMBRE_DEPART,REFERENCIA_DEPART) values ('AMAZONAS')

jdbc.properties:

#org.hibernate.dialect.PostgreSQLDialect
hibernate.default_schema = "DBMERCANCIAS"
hibernate.show_sql = true
hibernate.id.new_generator_mappings = true
hibernate.hbm2ddl.auto = create
hibernate.jdbc.batch_size = 5
#Default the factory to use to instantiate transactions     org.transaction.JDBCTransactionFactory
hibernate.transaction.factory_class=org.transaction.JDBCTransactionFactory
#Initialize values statements only on create-drop or create
hibernate.hbm2ddl.import_files = /import.sql    

数据库是postgresql 9.1.1,spring 3.1.0.RELEASE和hibernate 4.1.2.Final,hibernate.hbm2ddl.auto设置为“create”,表和schema创建但不运行sql命令insert为什么?,我可以在日志中看到这个命令运行的地方。

2 个答案:

答案 0 :(得分:4)

我的错误是hibernate属性中的位置。

hibernate.hbm2ddl.import_files = /META-INF/spring/import.sql

是正确的位置。

答案 1 :(得分:0)

您可以将import.sql放入classpath(/classes/import.sql)并从hibernate配置/属性中删除属性hibernate.hbm2ddl.import_files

注意: hibernate.hbm2ddl.auto必须创建

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
    </property>
</bean>