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
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)
MySQL version:
mysql Ver 14.14 Distrib 5.6.16, for osx10.7 (x86_64) using EditLine wrapper
Tomcat version:
apache-tomcat-8.0.22
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>
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; } }
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);
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?
We faced a similar problem after upgrading to Hibernate 5.x and this was solved by changing the dialect to org.hibernate.dialect.MySQL5Dialect.
try something like that in sessionFactoryInitialisation :
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure(configFile).build();
Metadata metadata =new MetadataSources(standardRegistry).getMetadataBuilder().build();
sessionFactory = metadata.getSessionFactoryBuilder().build();
return sessionFactory;
add <property name="javax.persistence.schema-generation.database.action">
create</property>
in your hibernate config file.
The correct property name is: hibernate.hbm2ddl.auto
Docs: https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch03.html#configuration-optional
来源:https://stackoverflow.com/questions/33449545/table-not-created-with-hbm2ddl-auto-update-in-hibernate-5