6

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

Java 8 + Tomcat 8 + MySQL + 休眠 5

  1. 爪哇版:

    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)
    
  2. MySQL版本:

    mysql  Ver 14.14 Distrib 5.6.16, for osx10.7 (x86_64) using  EditLine wrapper
    
  3. 雄猫版本:

    apache-tomcat-8.0.22
    
  4. pom.xml 片段:

    <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>
    
  5. 实体类:

    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; }
    }
    
  6. 休眠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);
    
  7. 休眠.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 + 休眠 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 中缺少什么?

4

3 回答 3

12

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

于 2017-06-09T09:18:50.500 回答
1

在您的休眠配置文件中添加<property name="javax.persistence.schema-generation.database.action">创建。</property>

于 2018-08-31T02:17:06.150 回答
0

在sessionFactoryInitialisation中尝试类似的操作:

StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure(configFile).build();       
Metadata metadata =new MetadataSources(standardRegistry).getMetadataBuilder().build();  
sessionFactory = metadata.getSessionFactoryBuilder().build();
return sessionFactory;
于 2017-03-16T15:39:32.340 回答