简体   繁体   中英

Hibernate JPA not executing import.sql on classpath

I have a Hibernate JPA project that has an import.sql file on the classpath. I have tried a number of things to get it working. My persistence.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="SNAPSHOTPU"
        transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:jboss/datasources/PostgresqlDS</jta-data-source>
        <class>net.example.Actor</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="javax.persistence.schema-generation.database.action"
                value="drop-and-create" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
            <property name="hibernate.hbm2ddl.import_files" value="import.sql" />
        </properties>
    </persistence-unit>
</persistence>

I have also tried it without the hibernate.hbm2ddl.* stuff because this is JPA, but neither seems to work. My import.sql is in src/main/resources and this is a Maven project.

I have one valid SQL statement in the file:

insert into actors values (1, 'Test Import.sql');

I even tried a blank line at the beginning of the file as I saw someone suggest. I have debug turned on for Hibernate and can see it running the DDL commands in the log to create the tables, but I do not see anything about import.sql or about the insert statement in the file.

Why isn't this working?

EDIT

@Baldurian got it right below, here is what works (in the persistence.xml):

<properties>
    <property name="hibernate.show_sql" value="true" />
    <property name="javax.persistence.schema-generation.database.action"
        value="drop-and-create" />
    <property name="javax.persistence.sql-load-script-source"
        value="/import.sql" />
</properties>

Try (change javax.persistence.sql-load-script-source path accordingly):

<properties>
    <property name="hibernate.show_sql" value="true" />
    <property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
    <property name="javax.persistence.schema-generation.create-source" value="metadata-then-script"/>
    <property name="javax.persistence.sql-load-script-source" value="/import.sql"/>
</properties>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM