Spring persistence.xml中的hibernate.hbm2ddl.import_文件不工作

Spring persistence.xml中的hibernate.hbm2ddl.import_文件不工作,spring,hibernate,junit,persistence.xml,Spring,Hibernate,Junit,Persistence.xml,我需要hibernate在启动junit测试之前读取sql文件,因此我在persistence.xml中进行了以下配置: <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.co

我需要hibernate在启动junit测试之前读取sql文件,因此我在persistence.xml中进行了以下配置:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
                             http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.connection.username" value="sa" />
        <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
        <property name="hibernate.connection.password" value="" />
        <property name="hibernate.connection.url" value="jdbc:hsqldb:hsql://localhost:9001/test" /> 
        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.id.new_generator_mappings" value="true" />
        <property name="hibernate.hbm2ddl.auto" value="create" />
        <property name="hibernate.hbm2ddl.import_files" value="/META-INF/load.sql" />
    </properties>
</persistence-unit>
</persistence>
当我运行junit测试时,没有导入文件load.sql,也没有显示任何错误。 我正在使用Hibernate 4和Spring 3.0.5。

我认为(我通常使用它)您应该将其包含在您的applicationContext.xml中:

<import resource="classpath:/path_to_persistence/persistence.xml" />

我通过将
load.sql
放入
src/test/resource/
文件夹解决了我的问题。
这种方法不需要在
persistence.xml
中指定sql文件的位置。

只需在
src/test/resources
目录中放置一个名为
import.sql
的文件即可

属性
javax.persistence.hibernate.hbm2ddl.import\u files
的默认值为
import.sql


检查:

首先,您没有在application-context.xml中指定persistence.xml的路径。第二,请展示您的junit测试,我们需要看看您是如何配置它的。谢谢你的回复。我认为没有必要在application-context.xml中指定persistence.xml的路径。Junit测试以前是有效的。他们停止工作,因为创建了一些触发器来启动序列,从而使自动递增。在运行junit测试之前,我必须使用load.sql创建这些触发器。我要求您进行junit配置(通过注释),以了解如何指定persistence.xml的路径。如果您不指定,您将在不使用Hibernate的情况下运行测试。我编辑了我的问题。如何指定persistence.xml的路径?你能给我举个例子吗?
org.xml.sax.SAXParseException;行号:7;栏目号:16;cvc elt.1:找不到元素“persistence”的声明。
我认为它无法解析,因为persistence.xml与您向我公开的applicationContext.xml.persistence.xml不同,它只是配置的一个片段。这还不够。看看这里应该怎么写我又编辑了我的问题。我所做的代码与您发送的代码完全相同,并且我得到了相同的错误。从您的代码中,我看到您没有在persistence.xml中关闭,我没有看到任何错误。请向我展示您的完整applicationContext.xml
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/applicationContext.xml" })
@TestExecutionListeners(inheritListeners = false, listeners = {
    TransactionalTestExecutionListener.class, DependencyInjectionTestExecutionListener.class })
@TransactionConfiguration(defaultRollback = true)
@Transactional
public abstract class UnitTestConfiguration {}
<import resource="classpath:/path_to_persistence/persistence.xml" />
@ContextConfiguration(locations = { "classpath:/applicationContext1.xml", ... ,"classpath:/applicationContextn.xml" })