开发者

Persistence doesn't create table from entity in database

开发者 https://www.devze.com 2023-03-16 11:49 出处:网络
I build a web application using hibernate JPA 2 + spring. I have problem with creation of domain model. In persistence I declared the automatic creation of database tables from entity开发者_开发技巧.

I build a web application using hibernate JPA 2 + spring. I have problem with creation of domain model. In persistence I declared the automatic creation of database tables from entity开发者_开发技巧. Here is my persistence.xml file:

<persistence-unit name="basicPersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
        <property name="hibernate.hbm2ddl.auto" value="create"/>
        <property name="hibernate.connection.charSet" value="UTF-8"/>
    </properties>
</persistence-unit>

In domain model I have two classes. One Is abstract which provide the persistence properties to inherited classes:

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.type.UUIDBinaryType;
import javax.persistence.*;

@MappedSuperclass
abstract class AbstractDomainObject implements DomainObject {

    private static final long serialVersionUID = 1L;

    private UUIDBinaryType id;

    private Long version;

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(length = 16, unique = true, nullable = false)
    public final UUIDBinaryType getId() {
        return id;
    }

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

    @Version
    public final Long getVersion() {
        return version;
    }

    public final void setVersion(Long version) {
        this.version = version;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof AbstractDomainObject)) return false;

        AbstractDomainObject that = (AbstractDomainObject) o;

        return !(id != null ? !id.equals(that.id) : that.id != null) &&
                !(version != null ? !version.equals(that.version) : that.version != null);
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (version != null ? version.hashCode() : 0);
        return result;
    }

}

Here is the concrete class which represent the entity:

import javax.persistence.Entity;

@Entity
public class Person extends AbstractDomainObject {

    private static final long serialVersionUID = 1L;

    private String name;

    private String surname;

    private Integer identifier;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public Integer getIdentifier() {
        return identifier;
    }

    public void setIdentifier(Integer identifier) {
        this.identifier = identifier;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Person)) return false;
        if (!super.equals(o)) return false;

        Person test = (Person) o;

        return !(name != null ? !name.equals(test.name) : test.name != null) &&
                !(surname != null ? !surname.equals(test.surname) : test.surname != null) &&
                !(identifier != null ? !identifier.equals(test.identifier) : test.identifier != null);

    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (surname != null ? surname.hashCode() : 0);
        result = 31 * result + (identifier != null ? identifier.hashCode() : 0);
        return result;
    }

}

Here is the interface which I'm using in abstract class:

import org.hibernate.type.UUIDBinaryType;
import java.io.Serializable;

public interface DomainObject extends Serializable {

    UUIDBinaryType getId();

    void setId(UUIDBinaryType id);

    Long getVersion();

    void setVersion(Long version);

    boolean equals(Object o);

    int hashCode();

}

And here is the log from tomcat with debug mode:

Jun 28, 2011 6:23:32 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive webFrontend-1.0-SNAPSHOT.war
2011-06-28 18:23:33,495 [Thread-29] INFO  org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
2011-06-28 18:23:33,530 [Thread-29] INFO  org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Tue Jun 28 18:23:33 CEST 2011]; root of context hierarchy
2011-06-28 18:23:33,583 [Thread-29] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from URL [file:/opt/apache-tomcat-7.0.16/webapps/webFrontend-1.0-SNAPSHOT/WEB-INF/classes/META-INF/spring/applicationContext.xml]
2011-06-28 18:23:33,709 [Thread-29] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from URL [file:/opt/apache-tomcat-7.0.16/webapps/webFrontend-1.0-SNAPSHOT/WEB-INF/classes/META-INF/spring/basicDataSource.xml]
2011-06-28 18:23:33,722 [Thread-29] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from URL [file:/opt/apache-tomcat-7.0.16/webapps/webFrontend-1.0-SNAPSHOT/WEB-INF/classes/META-INF/spring/applicationContext-persistence.xml]
2011-06-28 18:23:33,909 [Thread-29] INFO  org.springframework.beans.factory.config.PropertyPlaceholderConfigurer - Loading properties file from file [/opt/apache-tomcat-7.0.16/webapps/webFrontend-1.0-SNAPSHOT/WEB-INF/classes/META-INF/spring/database.properties]
2011-06-28 18:23:33,929 [Thread-29] INFO  org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2863725d: defining beans [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,basicDataSource,PersonDao,org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0,entityManagerFactory,transactionManager,org.springframework.transaction.config.internalTransactionAspect]; root of factory hierarchy
2011-06-28 18:23:33,995 [Thread-29] INFO  org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'basicPersistenceUnit'
2011-06-28 18:23:34,153 [Thread-29] INFO  org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
2011-06-28 18:23:34,160 [Thread-29] INFO  org.hibernate.cfg.Environment - Hibernate 3.6.3.Final
2011-06-28 18:23:34,161 [Thread-29] INFO  org.hibernate.cfg.Environment - hibernate.properties not found
2011-06-28 18:23:34,164 [Thread-29] INFO  org.hibernate.cfg.Environment - Bytecode provider name : javassist
2011-06-28 18:23:34,167 [Thread-29] INFO  org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
2011-06-28 18:23:34,243 [Thread-29] INFO  org.hibernate.ejb.Version - Hibernate EntityManager 3.6.3.Final
2011-06-28 18:23:34,261 [Thread-29] INFO  org.hibernate.ejb.Ejb3Configuration - Processing PersistenceUnitInfo [
    name: basicPersistenceUnit
    ...]
2011-06-28 18:23:34,359 [Thread-29] INFO  org.hibernate.cfg.Configuration - Hibernate Validator not found: ignoring
2011-06-28 18:23:34,373 [Thread-29] INFO  org.hibernate.cfg.search.HibernateSearchEventListenerRegister - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
2011-06-28 18:23:34,376 [Thread-29] INFO  org.hibernate.connection.ConnectionProviderFactory - Initializing connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
2011-06-28 18:23:34,378 [Thread-29] INFO  org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider - Using provided datasource
2011-06-28 18:23:34,666 [Thread-29] INFO  org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
2011-06-28 18:23:34,717 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Database ->
       name : MySQL
    version : 5.0.51a-24+lenny5
      major : 5
      minor : 0
2011-06-28 18:23:34,722 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Driver ->
       name : MySQL-AB JDBC Driver
    version : mysql-connector-java-5.1.16 ( Revision: ${bzr.revision-id} )
      major : 5
      minor : 1
2011-06-28 18:23:34,723 [Thread-29] INFO  org.hibernate.transaction.TransactionFactoryFactory - Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
2011-06-28 18:23:34,725 [Thread-29] INFO  org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
2011-06-28 18:23:34,725 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
2011-06-28 18:23:34,725 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
2011-06-28 18:23:34,725 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
2011-06-28 18:23:34,725 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
2011-06-28 18:23:34,726 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
2011-06-28 18:23:34,726 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled
2011-06-28 18:23:34,726 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Connection release mode: auto
2011-06-28 18:23:34,726 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Maximum outer join fetch depth: 2
2011-06-28 18:23:34,726 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
2011-06-28 18:23:34,726 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
2011-06-28 18:23:34,726 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
2011-06-28 18:23:34,726 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
2011-06-28 18:23:34,726 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
2011-06-28 18:23:34,728 [Thread-29] INFO  org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
2011-06-28 18:23:34,728 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
2011-06-28 18:23:34,728 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: enabled
2011-06-28 18:23:34,728 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
2011-06-28 18:23:34,728 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Query cache: disabled
2011-06-28 18:23:34,728 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
2011-06-28 18:23:34,729 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
2011-06-28 18:23:34,730 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
2011-06-28 18:23:34,733 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout
2011-06-28 18:23:34,734 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Statistics: disabled
2011-06-28 18:23:34,734 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
2011-06-28 18:23:34,734 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
2011-06-28 18:23:34,734 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Named query checking : enabled
2011-06-28 18:23:34,734 [Thread-29] INFO  org.hibernate.cfg.SettingsFactory - Check Nullability in Core (should be disabled when Bean Validation is on): enabled
2011-06-28 18:23:34,747 [Thread-29] INFO  org.hibernate.impl.SessionFactoryImpl - building session factory
2011-06-28 18:23:34,753 [Thread-29] INFO  org.hibernate.type.BasicTypeRegistry - Type registration [wrapper_characters_clob] overrides previous : org.hibernate.type.CharacterArrayClobType@32ee7cee
2011-06-28 18:23:34,753 [Thread-29] INFO  org.hibernate.type.BasicTypeRegistry - Type registration [characters_clob] overrides previous : org.hibernate.type.PrimitiveCharacterArrayClobType@474c0761
2011-06-28 18:23:34,753 [Thread-29] INFO  org.hibernate.type.BasicTypeRegistry - Type registration [clob] overrides previous : org.hibernate.type.ClobType@507895d8
2011-06-28 18:23:34,753 [Thread-29] INFO  org.hibernate.type.BasicTypeRegistry - Type registration [java.sql.Clob] overrides previous : org.hibernate.type.ClobType@507895d8
2011-06-28 18:23:34,753 [Thread-29] INFO  org.hibernate.type.BasicTypeRegistry - Type registration [blob] overrides previous : org.hibernate.type.BlobType@1cb5c12e
2011-06-28 18:23:34,754 [Thread-29] INFO  org.hibernate.type.BasicTypeRegistry - Type registration [java.sql.Blob] overrides previous : org.hibernate.type.BlobType@1cb5c12e
2011-06-28 18:23:34,754 [Thread-29] INFO  org.hibernate.type.BasicTypeRegistry - Type registration [materialized_clob] overrides previous : org.hibernate.type.MaterializedClobType@609dc1bb
2011-06-28 18:23:34,754 [Thread-29] INFO  org.hibernate.type.BasicTypeRegistry - Type registration [wrapper_materialized_blob] overrides previous : org.hibernate.type.WrappedMaterializedBlobType@151a0d8b
2011-06-28 18:23:34,754 [Thread-29] INFO  org.hibernate.type.BasicTypeRegistry - Type registration [materialized_blob] overrides previous : org.hibernate.type.MaterializedBlobType@616f2b7f
2011-06-28 18:23:34,803 [Thread-29] INFO  org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
2011-06-28 18:23:34,822 [Thread-29] INFO  org.hibernate.tool.hbm2ddl.SchemaExport - Running hbm2ddl schema export
2011-06-28 18:23:34,822 [Thread-29] INFO  org.hibernate.tool.hbm2ddl.SchemaExport - exporting generated schema to database
2011-06-28 18:23:34,827 [Thread-29] INFO  org.hibernate.tool.hbm2ddl.SchemaExport - schema export complete
2011-06-28 18:23:34,934 [Thread-29] INFO  org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 1438 ms
2011-06-28 18:23:34,962 [Thread-29] INFO  org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'webFrontendDispatcher': initialization started
2011-06-28 18:23:34,965 [Thread-29] INFO  org.springframework.web.context.support.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'webFrontendDispatcher-servlet': startup date [Tue Jun 28 18:23:34 CEST 2011]; parent: Root WebApplicationContext
2011-06-28 18:23:34,967 [Thread-29] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/web-config.xml]
2011-06-28 18:23:35,083 [Thread-29] INFO  org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@391c3288: defining beans [org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#0,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.config.viewControllerHandlerAdapter,org.springframework.web.servlet.config.viewControllerHandlerMapping]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@2863725d
2011-06-28 18:23:35,299 [Thread-29] INFO  org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/index.jsp] onto handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]
2011-06-28 18:23:35,385 [Thread-29] INFO  org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'webFrontendDispatcher': initialization completed in 422 ms

There is no error in this log and there is information about schema export was completely, but the table Person isn't created in database. Do I anything in wrong way?


Please try

 <prop key="hibernate.hbm2ddl.auto">update</prop> 

It will update ecisting schema and create the new one of it does not exist.

I'm working with MySQL too and haven't got any problem with that.


I solve this problem, I add tag to my persistence.xml file which contain class name of class which has @Entity annotation. But why I must add class like this way? In spring roo when I generate code, there is no tags in persistence.xml file


I had this problem because I named an entity "Group" which is a reserved word in MySQL (the database I was using: https://dev.mysql.com/doc/refman/5.5/en/reserved-words.html). I had to choose a different name for my entity/table/JSON object. I could see this in the Hibernate Output on startup as follows:

o.h.SQL: drop table if exists Group
o.h.t.h.SchemaExport: HHH000389: Unsuccessful: drop table if exists Group
o.h.t.h.SchemaExport: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Group' at line 1

Hibernate was otherwise silent on the problem...I think because reserved words are a bit of a swamp, though the Hibernate driver should pick it up.

Note:

the hibernate output is defined like this:

hibernate.show_sql : true

And the hibernate dialect (which ultimately determines the reserved words is defined like this:

hibernate.dialect: org.hibernate.dialect.MySQL5InnoDBDialect
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号