持久性不会在数据库中从实体创建表

时间:2011-06-28 16:25:41

标签: database hibernate spring persistence jpa-2.0

我使用hibernate JPA 2 + spring构建了一个Web应用程序。我有创建域模型的问题。在持久性中,我声明了从实体自动创建数据库表。这是我的persistence.xml文件:

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

在域模型中,我有两个类。一个是抽象的,为继承的类提供持久性属性:

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

}

这是代表实体的具体类:

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

}

这是我在抽象类中使用的接口:

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

}

这是来自tomcat的带调试模式的日志:

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

此日志中没有错误,并且有关于模式导出的信息已完全,但表中的Person不是在数据库中创建的。我有什么错误的方式吗?

3 个答案:

答案 0 :(得分:0)

请尝试

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

它将更新现有架构并创建新架构不存在。

我也在使用MySQL,并且没有遇到任何问题。

答案 1 :(得分:0)

我解决了这个问题,我将标签添加到我的persistence.xml文件中,该文件包含具有@Entity注释的类的类名。但为什么我必须这样添加类?在spring roo中,当我生成代码时,persistence.xml文件中没有标记

答案 2 :(得分:0)

我遇到了这个问题,因为我命名了一个实体“Group”,它是MySQL中的一个保留字(我正在使用的数据库:https://dev.mysql.com/doc/refman/5.5/en/reserved-words.html)。我必须为我的实体/表/ JSON对象选择一个不同的名称。我可以在启动时在Hibernate Output中看到这个:

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在这个问题上保持沉默...我认为因为保留字有点像沼泽,尽管Hibernate驱动程序应该把它拿起来。

<强> 注意:

hibernate输出定义如下:

hibernate.show_sql : true

hibernate方言(最终确定保留字的方式定义如下:

hibernate.dialect: org.hibernate.dialect.MySQL5InnoDBDialect