Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table not created with hbm2ddl.auto=update in Hibernate 5

The database table is NOT auto-created by the <property name="hbm2ddl.auto">update</property> settings in hibernate-cfg.xml, with the following combination:

Java 8 + Tomcat 8 + MySQL + Hibernate 5

  1. Java version:

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

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

    apache-tomcat-8.0.22
    
  4. pom.xml snippets:

    <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. Entity class:

    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. Hibernate SessionFactory initialization:

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

HOWEVER, the table was created into MySQL database, with the following combination:

Java 8 + Tomcat 8 + MySQL + Hibernate 4

Everything same as above, except in pom.xml, I was using hibernate 4 instead of 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>

What am I missing in Hibernate 5?

like image 203
Ming Avatar asked Oct 31 '15 08:10

Ming


People also ask

How does hibernate work in hbm2ddl?

hbm2ddl.auto Create : If the value is CREATE then the hibernate first drops the existing tables data and structure, then creates new tables and executes the operations on the newly created tables. <property name="hbm2ddl.auto">create</property>. The only problem with the value “create” is, we lose existing table data.

What is hbm2ddl auto update?

hbm2ddl.auto update : If the value is update then, Hibernate checks for the table and columns. If a table doesn’t exist then it creates new tables and where as if a column doesn’t exist it creates new columns for it. <property name="hbm2ddl.auto"> update </property>

How does hibernate update a table in SQL Server?

If the value is update then, Hibernate checks for the table and columns. If a table doesn’t exist then it creates new tables and where as if a column doesn’t exist it creates new columns for it. But in the case of value “ update ” hibernate doesn’t drop any existing table, so that we don’t lose existing table data.

What is auto update in hibernate?

hbm2ddl.auto update : If the value is update then, Hibernate checks for the table and columns. If a table doesn’t exist then it creates new tables and where as if a column doesn’t exist it creates new columns for it. But in the case of value “update” hibernate doesn’t drop any existing table, so that we don’t lose existing table data.


2 Answers

add <property name="javax.persistence.schema-generation.database.action">create</property> in your hibernate config file.

like image 25
suryakant sahu Avatar answered Oct 08 '22 04:10

suryakant sahu


We faced a similar problem after upgrading to Hibernate 5.x and this was solved by changing the dialect to org.hibernate.dialect.MySQL5Dialect.

like image 172
James Selvakumar Avatar answered Oct 08 '22 04:10

James Selvakumar