Hibernate 不生成从模型到数据库的表

Hibernate does not generate tables from models to database

我正在尝试从模型 class 中获取包含表的数据库,问题是自动生成带有 Hibernate 注释的表。

我有一个名为 models-test 的 Maven 模型项目,其中我有以下 class:

@Audited
@Entity
@Table(name = "test_EXTEND", schema = "COTest")
public class ComaTest implements Serializable {

private static final long serialVersionUID = 393765467600954104L;

@Id
@Column(name = "CL_o_CODE")
private Integer id;
@Column(name = "CL_o_NUM01")
private Long refCountryCode;
@Column(name = "CL_o_STR01")
private String city;
@Column(name = "CL_o_STR02")
private String zipCode;

public Integer getId() {
    return this.id;
}

public void setId(final Integer idVal) {
    this.id = idVal;
}

public Long getRefCountryCode() {
    return this.refCountryCode;
}
public void setRefCountryCode(final Long refCountryCode) {
    this.refCountryCode = refCountryCode;
}
public String getCity() {
    return this.city;
}
public void setCity(final String cityVal) {
    this.city = cityVal;
}
public String getZipCode() {
    return this.zipCode;
}
public void setZipCode(final String zipCodeVal) {
    this.zipCode = zipCodeVal;
}
}

我有一个名为 interface_test 的其他项目,我有我所有的接口,然后我有第三个 maven projet 命名服务测试,我在 persistence.xml fil:

<persistence-unit name="op_PU">
<jta-data-source>java:jboss/datasources/op_DS</jta-data-source>
<properties>            
        <property name="hibernate.show_sql" value="false" />            
        <property name="hibernate.hbm2ddl.auto" value="update" />

        <property name="org.hibernate.envers.audit_table_suffix"
            value="_aud" />
        <property name="org.hibernate.envers.revision_field_name"
            value="rev" />
        <property name="org.hibernate.envers.revision_type_field_name"
            value="rev_type" />
    </properties>
</persistence-unit>

在 jboss 服务器的 standalone.xml 中,我有这个连接字符串:

<datasource jndi-name="java:jboss/datasources/op_DS" pool-name="op_DS" enabled="true" use-java-context="true">
                <connection-url>jdbc:sqlserver://localhost;databaseName=OP_DB;</connection-url>
                <driver>sqlserver</driver>
                <security>
                    <user-name>sa</user-name>
                    <password>testpassword</password>
                </security>
</datasource>

在控制台中我有:

ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 94) The specified schema name "COTest" either does not exist or you do not have permission to use it.
ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 94) HHH000389: Unsuccessful: create table COTest.test_EXTEND (CL_o_CODE int not null, CL_o_STR01 varchar(255), CL_o_STR02 varchar(255), CL_o_STR03 varchar(255), primary key (CL_o_CODE, rev)))

您好,我认为您的 persistence.xml 属性 名为 hibernate.hbm2ddl.auto 存在问题,您已为其提供更新值,而您需要的是创建或创建删除(如果是开发环境)生成从模型到数据库的表。 休眠的可能选项列表。hbm2ddl.auto 是,

validate: validate the schema, makes no changes to the database.
update: update the schema.
create: creates the schema, destroying previous data.
create-drop: drop the schema at the end of the session.

当我在 SQL Server 2008 R2 上手动创建模式时,hibernate 可以完美地从现有实体生成表。 这是关于持久化单元的代码块:

<persistence-unit name="OP_PU">
<jta-data-source>java:jboss/datasources/OP_DS</jta-data-source>
 <properties>
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.hbm2ddl.auto" value="create" />



        <property name="org.hibernate.envers.audit_table_suffix"
            value="_aud" />
        <property name="org.hibernate.envers.revision_field_name"
            value="rev" />
        <property name="org.hibernate.envers.revision_type_field_name"
            value="rev_type" />

    </properties>
</persistence-unit>

但我正在寻找一种解决方案,它可以帮助我使用模式名称自动创建表,而无需手动创建模式(在生成表之前,它必须先创建模式) 有解决方案吗?

好的,在看到你的最新 post 之后,我想你必须做一些事情,我们可以在创建表之前创建模式: 1. 就像将“;INIT=create schema IF NOT EXISTS MYDBNAME”添加到您的数据库 URL 例如

database.url=jdbc:DB:USER:;INIT=create schema IF NOT EXISTS MYDBNAME.

您可以选择使用文件路径将 RUNSCRIPT 添加到 INIT 值

database.url=jdbc:DB:USER:;INIT=RUNSCRIPT FROM 'src/create.sql';
  1. 使用休眠你可以做上面的事情

一个。在资源中添加一个 create.sql 文件,内容如下:

/*create database at first time*/ 
create schema IF NOT EXISTS MYDBNAME;

b。并添加 属性 "hbm2ddl.import_files" 到 "hibernate.cfg.xml"",如下所示:

<hibernate-configuration>
    <session-factory>   
       <property name="hbm2ddl.import_files">path/create.sql</property>
       other properties
    </session-factory>
</hibernate-configuration>

所以,如果数据库不存在,hibernate 会创建新的数据库。