在 Hibernate 5 中未使用 hbm2ddl.auto=update 创建表

Posted

技术标签:

【中文标题】在 Hibernate 5 中未使用 hbm2ddl.auto=update 创建表【英文标题】:Table not created with hbm2ddl.auto=update in Hibernate 5 【发布时间】:2016-01-31 16:18:07 【问题描述】:

数据库表不是hibernate-cfg.xml 中的<property name="hbm2ddl.auto">update</property> 设置自动创建的,具有以下组合:

Java 8 + Tomcat 8 + MySQL + Hibernate 5

    Java 版本:

    java version "1.8.0_45"
    Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
    Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
    

    mysql 版本:

    mysql  Ver 14.14 Distrib 5.6.16, for osx10.7 (x86_64) using  EditLine wrapper
    

    Tomcat 版本:

    apache-tomcat-8.0.22
    

    pom.xml sn-ps:

    <dependency>
      <groupId>org.eclipse.persistence</groupId>
      <artifactId>javax.persistence</artifactId>
      <version>2.1.0</version>
    </dependency>
    <dependency>
      <groupId>javax.transaction</groupId>
      <artifactId>jta</artifactId>
      <version>1.1</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate.javax.persistence</groupId>
      <artifactId>hibernate-jpa-2.1-api</artifactId>
      <version>1.0.0.Final</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.0.3.Final</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>5.0.3.Final</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-c3p0</artifactId>
      <version>5.0.3.Final</version>
    </dependency>
    

    实体类:

    package com.test.entity;
    
    import java.util.Date;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;
    
    import org.hibernate.annotations.GenericGenerator;
    
    @Entity
    @Table( name = "EVENTS" )
    public class Event 
        @Id
        @GeneratedValue(generator="increment")
        @GenericGenerator(name="increment", strategy = "increment")
        private Long id;
    
        private String title;
    
        @Temporal(TemporalType.TIMESTAMP)
        @Column(name = "EVENT_DATE")
        private Date date;
    
        public Event() 
    
        public Long getId()  return id; 
        private void setId(Long id)  this.id = id; 
        public Date getDate()  return date; 
        public void setDate(Date date)  this.date = date; 
        public String getTitle()  return title; 
        public void setTitle(String title)  this.title = title; 
    
    

    休眠SessionFactory初始化:

    String resource = "hibernate.cfg.xml";
    Configuration configuration = new Configuration();
    configuration.configure(resource);
    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
    SessionFactory sessionFactoryCore = configuration.buildSessionFactory(serviceRegistry);
    

    hibernate.cfg.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC 
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
    <hibernate-configuration>
      <session-factory>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">mysqluser</property>
        <property name="connection.password">******</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="hibernate.max_fetch_depth">3</property>
        <mapping class="com.test.entity.Event" />
      </session-factory>
    </hibernate-configuration>
    

但是,该表是在 MySQL 数据库中创建的,具有以下组合:

Java 8 + Tomcat 8 + MySQL + Hibernate 4

一切都和上面一样,除了在 pom.xml 中,我使用的是 hibernate 4 而不是 5:

<dependency>
  <groupId>org.eclipse.persistence</groupId>
  <artifactId>javax.persistence</artifactId>
  <version>2.1.0</version>
</dependency>
<dependency>
  <groupId>javax.transaction</groupId>
  <artifactId>jta</artifactId>
  <version>1.1</version>
</dependency>
<dependency>
  <groupId>org.hibernate.javax.persistence</groupId>
  <artifactId>hibernate-jpa-2.1-api</artifactId>
  <version>1.0.0.Final</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>4.3.11.Final</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>4.3.11.Final</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-c3p0</artifactId>
  <version>4.3.11.Final</version>
</dependency>

我在 Hibernate 5 中缺少什么?

【问题讨论】:

【参考方案1】:

我们在升级到 Hibernate 5.x 后遇到了类似的问题,通过将 dialect 更改为 org.hibernate.dialect.MySQL5Dialect 解决了这个问题。

【讨论】:

【参考方案2】:

在您的休眠配置文件中添加&lt;property name="javax.persistence.schema-generation.database.action"&gt;create&lt;/property&gt;

【讨论】:

一些解释会大大提高答案的质量,帮助以后可能遇到类似问题的其他用户请花点时间阅读帮助页面***.com/help/how-to-answer【参考方案3】:

sessionFactoryInitialisation 中尝试类似的操作:

StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure(configFile).build();       
Metadata metadata =new MetadataSources(standardRegistry).getMetadataBuilder().build();  
sessionFactory = metadata.getSessionFactoryBuilder().build();
return sessionFactory;

【讨论】:

如何解决自动创建问题?

以上是关于在 Hibernate 5 中未使用 hbm2ddl.auto=update 创建表的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate自动建表失败原因总结

Hibernate批量操作

Alamofire 5中未设置授权标头?

Xcode 5.0.1 中未报告分段错误

Laravel 5.6 通配符路由在域中未按预期运行

Laravel 5.1 中未定义的默认命名空间