1

using Hibernate tools, I want to generate a script to create a table from an Entity in my java project. do you know the way to do it?

import java.io.Serializable; 
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "foo")
public class Foo implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    private int userId;
    @Id
    private int number;

    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number= number;
    }
}

I'm trying with this very simple example

xedo
  • 1,117
  • 3
  • 18
  • 34
  • May your answer in these links [1]: http://stackoverflow.com/questions/10778755/hibernates-hbm2ddl-auto-property-with-value-create-is-not-re-creating-table#autocomment30537303 [2] http://stackoverflow.com/questions/306806/hibernate-automatically-creating-updating-the-db-tables-based-on-entity-classes – Kanhu Bhol Dec 07 '13 at 12:34
  • no, I just want create a script automatically to create the table – xedo Dec 09 '13 at 13:59

3 Answers3

1

Do like this

    String[] s = config.generateSchemaCreationScript(new PostgreSQL82Dialect());
    StringBuilder script = new StringBuilder();
    Formatter formatter = FormatStyle.DDL.getFormatter();
    for (int i = 0; i < s.length; i++) {           
        String line = formatter.format(s[i]);            
        script.append(line);
        script.append(";\n");
    }
    System.out.println(script.toString());
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
0

If you're using Maven, you can do it adding the code below to your pom.xml:

 <build>
  <plugins>
    <plugin>
      <!-- run "mvn hibernate3:hbm2ddl" to generate a schema -->
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>hibernate3-maven-plugin</artifactId>
      <version>2.2</version>
      <configuration>
        <components>
          <component>
            <name>hbm2ddl</name>
            <implementation>jpaconfiguration</implementation>
          </component>
        </components>
        <componentProperties>
          <persistenceunit>Default</persistenceunit>
          <outputfilename>schema.ddl</outputfilename>
          <drop>false</drop>
          <create>true</create>
          <export>false</export>
          <format>true</format>
        </componentProperties>
      </configuration>
    </plugin>
    <!-- other plugin configurations ... -->
  </plugins>
</build>

You can find all information in: http://users.mafr.de/~matthias/articles/generating-ddl-scripts.html

Amin Abu-Taleb
  • 4,423
  • 6
  • 33
  • 50
0

SOLVED:
with the Hibernate perspective:

  1. add a configuration
    enter image description here

  2. generation code configuration
    enter image description here

  3. check a schema export and add properties
    enter image description here

xedo
  • 1,117
  • 3
  • 18
  • 34