简体   繁体   English

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

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

I'm trying to get a database with tables from models class, the problem comes with auto generating the tables with Hibernate annotations. 我正在尝试从models类中获取包含表的数据库,问题在于自动生成带有Hibernate批注的表。

I have a maven models projet nammed models-test where i have the following class: 我有一个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;
}
}

i have an other projets nammed interface_test where i have all my interfaces, then i have a third maven projet nammed services-test where i made in the persistence.xml fil: 我有另一个命名为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>

in the standalone.xml of jboss server i have this connection string : 在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>

In the consol i have that: 在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)))

Hi I think there is a issue with your persistence.xml property named hibernate.hbm2ddl.auto for which you have given value as update and what you need is create or create-drop(if development environment)to generate tables from models to database. 嗨,我认为您的persistence.xml属性名为hibernate.hbm2ddl.auto存在问题,您已为其提供了作为更新的值,而您需要的是创建或创建放置(如果是开发环境)以从模型到数据库生成表。 the list of possible options for hibernate.hbm2ddl.auto are, 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.

when i create the schema on SQL Server 2008 R2 manually, hibernate can generates perfectelly the tables from the existing entities. 当我在SQL Server 2008 R2上手动创建架构时,hibernate可以从现有实体中完美地生成表。 This is the code blocs about persistence unit: 这是关于持久性单元的代码块:

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

But i am looking for a solution whitch helps me to create tables automatically with schema name whitout creating schema manually (before gerenating tables , it must create schema first) Is there a solution to do that ? 但是我正在寻找一种解决方案,它可以帮助我自动创建带有模式名称的表,而无需手动创建模式(在生成表之前,必须先创建模式)是否有解决方案?

Ok after seeing your latest post ,I think you will have to do something in which we can create schema before creating tables: 1. Like adding ";INIT=create schema IF NOT EXISTS MYDBNAME"" to your database URL eg 好的,在看完您的最新文章之后,我认为您必须做一些事情,我们才能在创建表之前创建模式:1.像在数据库URL中添加“; INIT = create schema IF NOT EXISTS MYDBNAME”一样,例如

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

You have a option to add RUNSCRIPT to INIT value with the files paths 您可以选择使用文件路径将RUNSCRIPT添加到INIT值

database.url=jdbc:DB:USER:;INIT=RUNSCRIPT FROM 'src/create.sql';
  1. Using hibernate you can do above thing as 使用休眠,你可以做以上的事情

a. 一种。 Add a create.sql file in resource with following : 使用以下命令在资源中添加一个create.sql文件:

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

b. and add a property "hbm2ddl.import_files" to "hibernate.cfg.xml"" as follow: 并将属性“ 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>

So, if database not exist, hibernate creates new database. 因此,如果数据库不存在,休眠将创建新数据库。

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

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