views:

316

answers:

1

I have parametrized persistence.xml. I am trying to generate ddl schema using hbm2ddl. How can i pass parameters to this tool ?

My persistence.xml looks like

<property name="hibernate.connection.driver_class" value="${persistence.connection.driver.class}"/>
<property name="hibernate.dialect" value="${persistence.dialect}"/>
<property name="hibernate.connection.password" value="${persistence.password}"/>
<property name="hibernate.connection.username" value="${persistence.username}"/>

When i start server parameter values are passed as JAVA_OPTS (using -Dpersistence.dialect=value). And it works well.

How do i do this with hbm2ddl ?

I tried property

<hibernatetool destdir="${gensrc.sql.dir}">
  <property key="persistence.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
  <jpaconfiguration persistenceunit="${persistence.unit.name}" />
  <classpath>
   <!-- it is in this classpath you put your classes dir,
       and/or jpa persistence compliant jar -->
    <path location="${build.classes.dir}" />
  </classpath>
  <hbm2ddl export="false" drop="true" outputfilename="create_${ant.project.name}.sql" format="true" haltonerror="true" />
</hibernatetool>

But it does not get this value. It shows me error.

build.xml:160: org.hibernate.HibernateException: Dialect class not found: ${persistence.dialect}
+1  A: 

You could specify the dialect via propetyfile. Declare it in a hibernate.properties:

hibernate.dialect=org.hibernate.dialect.Oracle9Dialect

And use it like this:

<jpaconfiguration propertyfile="hibernate.properties"/>

Or try to declare the property inside the jpaconfiguration element (I didn't test this):

<jpaconfiguration>
  <property key="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
</jpaconfiguration>
Pascal Thivent
Thanks Pascal. I was referring to https://www.hibernate.org/hib_docs/tools/reference/en/html/ant.htmlHow can i do this using ant ? I mean can you point me where i should specify these properties in my <hibernatetools> on <hbm2ddl>Thanks.
Jigar Shah
@Jigar Sorry, wrong link. I've updated my anwser with others suggestions.
Pascal Thivent
@Pascal Thanks again. It worked. (first one). (jpaconfiguration does not support prop attribute. )Small issues still remains. Purpose of making persistence.xml parameterised was to change this value at run time. So if want to generate ddl for oracle, i can just specify a param with dialect and it generates ddl. Here i have to specify a file with hibernate.dialect=oracle.driver. I have done that for now and it works. Thanks for your reply.
Jigar Shah