Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Have a look at <a href="http://onpersistence.blogspot.com/2010/03/running-sql-script-on-startup-in.html" rel="noreferrer">Running a SQL Script on startup in EclipseLink</a> that describes a solution presented as a kind of equivalent to Hibernate's <code>import.sql</code> feature<sup>1</sup>. Credits to Shaun Smith:</p> <blockquote> <h2><a href="http://onpersistence.blogspot.com/2010/03/running-sql-script-on-startup-in.html" rel="noreferrer">Running a SQL Script on startup in EclipseLink</a></h2> <p>Sometimes, when working with DDL generation it's useful to run a script to clean up the database first. In Hibernate if you put a file called "import.sql" on your classpath its contents will be sent to the database. Personally I'm not a fan of magic filenames but this can be a useful feature.</p> <p>There's no built in support for this in EclipseLink but it's easy to do thank's to EclipseLink's high extensibility. Here's a quick solution I came up with: I simply register an event listener for the session postLogin event and in the handler I read a file and send each SQL statement to the database--nice and clean. I went a little further and supported setting the name of the file as a persistence unit property. You can specify this all in code or in the persistence.xml.</p> <p>The <code>ImportSQL</code> class is configured as a <code>SessionCustomizer</code> through a persistence unit property which, on the <code>postLogin</code> event, reads the file identified by the "import.sql.file" property. This property is also specified as a persistence unit property which is passed to <code>createEntityManagerFactory</code>. This example also shows how you can define and use your own persistence unit properties.</p> <pre><code>import org.eclipse.persistence.config.SessionCustomizer; import org.eclipse.persistence.sessions.Session; import org.eclipse.persistence.sessions.SessionEvent; import org.eclipse.persistence.sessions.SessionEventAdapter; import org.eclipse.persistence.sessions.UnitOfWork; public class ImportSQL implements SessionCustomizer { private void importSql(UnitOfWork unitOfWork, String fileName) { // Open file // Execute each line, e.g., // unitOfWork.executeNonSelectingSQL("select 1 from dual"); } @Override public void customize(Session session) throws Exception { session.getEventManager().addListener(new SessionEventAdapter() { @Override public void postLogin(SessionEvent event) { String fileName = (String) event.getSession().getProperty("import.sql.file"); UnitOfWork unitOfWork = event.getSession().acquireUnitOfWork(); importSql(unitOfWork, fileName); unitOfWork.commit() } }); } </code></pre> <hr> <pre><code>public static void main(String[] args) { Map&lt;String, Object&gt; properties = new HashMap&lt;String, Object&gt;(); // Enable DDL Generation properties.put(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.DROP_AND_CREATE); properties.put(PersistenceUnitProperties.DDL_GENERATION_MODE, PersistenceUnitProperties.DDL_DATABASE_GENERATION); // Configure Session Customizer which will pipe sql file to db before DDL Generation runs properties.put(PersistenceUnitProperties.SESSION_CUSTOMIZER, "model.ImportSQL"); properties.put("import.sql.file","/tmp/someddl.sql"); EntityManagerFactory emf = Persistence .createEntityManagerFactory("employee", properties); } </code></pre> </blockquote> <p>I'm not sure it's a strict equivalent though, I'm not sure the script will run after the database generation. Testing required. If it doesn't, maybe it can be adapted.</p> <p><sub><sup>1</sup> Hibernate has a neat little feature that is heavily under-documented and unknown. You can execute an SQL script during the SessionFactory creation right after the database schema generation to import data in a fresh database. You just need to add a file named import.sql in your classpath root and set either create or create-drop as your hibernate.hbm2ddl.auto property.</sub></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload