简体   繁体   中英

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.

I have a maven models projet nammed models-test where i have the following 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;
}
}

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:

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

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

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. the list of possible options for hibernate.hbm2ddl.auto are,

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

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

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 database at first time*/ 
create schema IF NOT EXISTS MYDBNAME;

b. and add a property "hbm2ddl.import_files" to "hibernate.cfg.xml"" as follow:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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