Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Hibernate import.sql to load?

Database tables are created successfully by

<property name="hibernate.hbm2ddl.auto" value="create"/>"

in persistence.xml, however each time I run my application (within Eclipse Helios), the import.sql doesn't appear to be loading. My persistence.xml file

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/ ... ce_2_0.xsd" version="2.0">
<persistence-unit name="myPersistenceUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create"/>

<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>

<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/svrmonitor4" />
<property name="hibernate.connection.username" value="username" />
<property name="hibernate.connection.password" value="password" />

</properties>   
</persistence-unit>
</persistence>

With the contents of import.sql I have tried removing line breaks, with+without semi colons ";", tested the SQL on the command line (the insert statements work), tried intentionally broken SQL (produces no errors on console). I have tried placing import.sql at the root of my classpath, and also in META-INF (still no luck). I have tried putting "USE svrmonitor4;" at the start of the import.sql file (again, no luck).

Am I missing something in persistence.xml?

If errors were encountered upon inserting to the MySQL database, where are the errors reported? Eclipse console, or MySQL logs? I haven't looked at MySQL logs yet.

In Eclipse I have three projects, with dependencies as follows:

Project A (contains Main class from which the app is launched) --> Project B --> Project C (contains Entity classes, persistence.xml)

I have tried placing import.sql in the classpath of each of these projects, but it makes no difference.

like image 352
willtardy Avatar asked Mar 29 '11 02:03

willtardy


People also ask

What is SQL init mode always?

This property takes one of three values: always – always initialize the database. embedded – always initialize if an embedded database is in use. This is the default if the property value is not specified. never – never initialize the database.

Which file by default contains database initialization DML scripts?

Spring Boot application can create DDL script schemas by using the JDBC Data source. The data-source connection factory automatically creates and initializes the DML scripts. This also loads the SQL as part of the standard classpath scanning for sql files, i.e. schema.


1 Answers

I found the problem by increasing the Log4J logging level, so that I could see Hibernate's hbm2ddl tool's INFO messages on the console. The import.sql file was fine in the root of the classpath of my project with the persistence.xml file, even though it was referenced by another invoking project within eclipse. The relevant lines I added to my log4j.xml:

<logger name="org.hibernate.tool.hbm2ddl" additivity="false">
    <level value="info"/><appender-ref ref="console" /> 
</logger>

<logger name="org.hibernate" additivity="false">
    <level value="warn"/><appender-ref ref="console" /> 
</logger>

This meant that I didn't get all the debug info from Hibernate, only that relevant to importing .import.sql.

Using Log4j is very useful for this because it shows me any errors with the SQL of the import.sql script

As it turned out, my SQL "INSERT INTO ..." statement didn't have the values in the right order (this happened because when you generate Tables from Entity classes, the order of the tables is alphabetical, where's the classes defined variables in my custom order).

like image 78
willtardy Avatar answered Sep 20 '22 10:09

willtardy