从实体生成表

时间:2013-12-06 13:31:48

标签: java hibernate jpa entity

使用Hibernate工具,我想生成一个脚本来从我的java项目中的Entity创建一个表。 你知道怎么做吗?

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;
    }
}

我正在尝试这个非常简单的例子

3 个答案:

答案 0 :(得分:1)

这样做

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

答案 1 :(得分:0)

如果您使用的是Maven,可以将以下代码添加到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>

您可以在http://users.mafr.de/~matthias/articles/generating-ddl-scripts.html

中找到所有信息

答案 2 :(得分:0)

解决


使用Hibernate透视图:

  1. 添加配置
    enter image description here

  2. 生成代码配置
    enter image description here

  3. 检查架构导出并添加属性
    enter image description here