-1
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="safety" transaction-type="JTA">
    <jta-data-source>java:/jdbc/MyDataSource</jta-data-source>
    <properties>
      <property name="javax.persistence.schema-generation.scripts.action" value="create"/>
      <property name="javax.persistence.schema-generation.scripts.create-target" value="create.ddl"/>
      <property name="hibernate.default_schema" value="safety"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.format_sql" value="false"/>
      <property name="hibernate.use_sql_comments" value="true"/>
    </properties>
  </persistence-unit>
</persistence>

This is my persistence.xml. How can I generate DB schema by Persistence.generateSchema()?

public class SchemaGenerator {
    public static void main(String args[]) {
        Map<String, Object> properties = new HashMap<String, Object>();
        //adding some properties
        Persistence.generateSchema("safety", properties);
    }
}

It gives me this error : org.hibernate.engine.jndi.JndiException: Error parsing JNDI name [java:/jdbc/MyDataSource].

But, it does not have to connect my database as I just need to generate ddl.

Chris
  • 199
  • 2
  • 13
  • I looked at the link below. But it does not work in my case. https://github.com/hantsy/ee7-sandbox/wiki/jpa-scripts – Chris Sep 22 '15 at 07:20
  • You can comment out the jta-data-source from persistence.xml for a temporary fix. – K.Nicholas Feb 14 '16 at 02:04

1 Answers1

0

You will have to set the property connection.provider_class to org.hibernate.service.jdbc.connections.internal.UserSuppliedConnectionProviderImpl. That way he is not trying to load the JNDI source.

This is my generation.properties I use for offline schema generation with the Ant Hibernate Tool :

hibernate.dialect=...
hibernate.connection.provider_class=org.hibernate.service.jdbc.connections.internal.UserSuppliedConnectionProviderImpl
javax.persistence.validation.mode=ddl, callback
hibernate.cache.use_second_level_cache=false
hibernate.cache.use_query_cache=false
Tobias Liefke
  • 8,637
  • 2
  • 41
  • 58
  • I've got this error. Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.service.jdbc.connections.internal.UserSuppliedConnectionProviderImpl] as strategy [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider] – Chris Sep 22 '15 at 08:19
  • In that case I guess your Hibernate version does not include that provider - which version are you using? – Tobias Liefke Sep 22 '15 at 08:25
  • If I remove jta-data-source from persistence.xml, it gives me this error: Exception in thread "main" java.lang.UnsupportedOperationException: The application must supply JDBC connections at org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProviderImpl.getConnection(UserSuppliedConnectionProviderImpl.java:61). Obviously, my library has that class. I am using hibernate 4.3.10.Final. – Chris Sep 22 '15 at 23:47
  • You should check, why he is calling `getConnection`. Can't see that from your stacktrace snippet. – Tobias Liefke Sep 23 '15 at 09:49