【问题标题】:Generate a Table from an Entity从实体生成表
【发布时间】:2013-12-23 21:07:52
【问题描述】:

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

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

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

【问题讨论】:

标签: java hibernate jpa entity


【解决方案1】:

已解决:
使用 Hibernate 视角:

  1. 添加配置

  2. 生成代码配置

  3. 检查架构导出并添加属性

【讨论】:

    【解决方案2】:

    如果您使用的是 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

    【讨论】:

      【解决方案3】:

      这样做

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

      【讨论】:

      • 在哪里?不是在类中右键单击并生成它吗?
      • @xedo 我不知道。
      猜你喜欢
      • 2012-04-28
      • 2013-05-10
      • 2020-11-30
      • 2017-12-16
      • 2013-01-31
      • 2017-08-06
      • 1970-01-01
      • 2020-11-16
      相关资源
      最近更新 更多