xedo
xedo

Reputation: 1147

Generate a Table from an Entity

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

Upvotes: 1

Views: 234

Answers (3)

xedo
xedo

Reputation: 1147

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

Upvotes: 0

Amin Abu-Taleb
Amin Abu-Taleb

Reputation: 4501

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

Upvotes: 0

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

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());

Upvotes: 1

Related Questions