繁体   English   中英

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

[英]Hibernate does not generate tables from models to database

我正在尝试从models类中获取包含表的数据库,问题在于自动生成带有Hibernate批注的表。

我有一个Maven模型Projet命名模型-测试 ,我有以下课程:

@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的projets,其中有我所有的接口,然后还有另一个在persistence.xml fil中进行的maven projet命名服务-test:

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

在consol我有:

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存在问题,您已为其提供了作为更新的值,而您需要的是创建或创建放置(如果是开发环境)以从模型到数据库生成表。 hibernate.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>

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

好的,在看完您的最新文章之后,我认为您必须做一些事情,我们才能在创建表之前创建模式:1.像在数据库URL中添加“; INIT = create schema IF NOT EXISTS MYDBNAME”一样,例如

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;

并将属性“ 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>

因此,如果数据库不存在,休眠将创建新数据库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM