0

i want to generate the Create Table Script for a specific entity Class.

I can auto-generate the complete Script for all of my classes with hibernate (hbm2dll tool) ( Auto generate data schema from JPA annotated entity classes ) but i don't know how to filter this so only the part that relates to one entity class remains.

Greetings, Laures

Community
  • 1
  • 1
Laures
  • 5,389
  • 11
  • 50
  • 76

3 Answers3

0

Are there no associations declared between your entities? Hibernate will generate Foreign Keys and mapping tables etc based on knowledge of your domain model.

If you just wanted the DDL for a single isolated table you could configure your session factory with just that entity and point it at an empty database. You can then record the DDL from the logs (generated by Hibernate as you mention above) and then delete this database when you've got what you need.

However if there any associations then hibernate will not be able to do this.

Alex Barnes
  • 7,174
  • 1
  • 30
  • 50
  • unfortunately the entity is not stand alone (that would be simple). But the referenced classes (or more correctly their tables) allready exist. It would be ok if the create table statements would use "if not exists" but unfortunately they dont. – Laures May 26 '11 at 15:53
  • Is this a new table? If you use update instead of create it will create the new table with all the foreign keys and references.. If this is the only domain change then hibernate will just create the new table and leave the existing ones alone. – Alex Barnes May 26 '11 at 22:12
0

I guess if you configure your persistence.xml file with that single entity, exclude all other unlisted entities, and then start your entity manager factory with the DDL auto generation enabled, you might get just that.

<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_2_0.xsd" version="2.0">
   <persistence-unit name="stackoverflow" transaction-type="RESOURCE_LOCAL">
        <class>com.stackoverflow.model.SingleEntity</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="create"/>
        </properties>
   </persistence-unit>
</persistence>
Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
0

I handled the script generation in code myself. the base queries are generated by calling the hbm2ddl tool and my code adds some details to them (like adding if not exists to the create queries; adding certain create index queries; ...)

Laures
  • 5,389
  • 11
  • 50
  • 76