Search code examples
javahibernatemavenjboss7.xhibernate-mapping

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

Solution

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