I am developing J2SE Application with Hibernate and HSQL database and I want db schema to be created programatically.
I following EJB3 - JPA approach and using persistence.xml as show below.
Code:
<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/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="astrosoft" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<!-- SQL stdout logging -->
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="use_sql_comments" value="true"/>
<property name="hibernate.connection.driver_class"
value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.url"
value="jdbc:hsqldb:file:db/astrodb;hsqldb.default_table_type=cached"/>
<!-- <property name="hibernate.connection.url"
value="jdbc:hsqldb:hsql://localhost"/> -->
<property name="hibernate.connection.username"
value="sa"/>
<property name="hibernate.connection.shutdown"
value="true"/>
<property name="hibernate.c3p0.min_size"
value="5"/>
<property name="hibernate.c3p0.max_size"
value="20"/>
<property name="hibernate.c3p0.timeout"
value="300"/>
<property name="hibernate.c3p0.max_statements"
value="50"/>
<property name="hibernate.c3p0.idle_test_period"
value="3000"/>
<property name="hibernate.dialect"
value="org.hibernate.dialect.HSQLDialect"/>
<!-- <property name="hibernate.hbm2ddl.auto" value="create" /> -->
</properties>
</persistence-unit>
</persistence>
To Use SchemaExport API, I need hibernate configuration, so I am trying to get hibernate cfg out of EJB 3 configuration. This is what I tried..
Code:
Ejb3Configuration cfg = new Ejb3Configuration().configure("/META-INF/persistence.xml");
Configuration hbmcfg = cfg.getHibernateConfiguration();
SchemaExport schemaExport = new SchemaExport(hbmcfg);
schemaExport.create(true, true);
But I am getting following error:
SEVERE: Error parsing XML: /META-INF/persistence.xml(1) Document is invalid: no grammar found.
Jun 4, 2007 4:24:43 PM org.hibernate.util.XMLHelper$ErrorLogger error
SEVERE: Error parsing XML: /META-INF/persistence.xml(1) Document root element "persistence", must match DOCTYPE root "null".
Please help me in knowing right way of exporting schema using EJB3/hibernate. My basic requirement is tables to be created automatically only if they does not exist, Please let me know if there are better way of doing it...
Any help would be very much appreciated..
Thanks,
Raja.