我创建了SpringBoot 1.5.11.RELEASE
和Hibernate 5 project
。一切都很好。
但是,当我将SpringBoot
版本更改为2.0.0.RELEASE
时,spring.jpa.hibernate.ddl-auto=create
无效。我试过this stackoverflow post,但没有运气。
以下是SpringBoot 1.5.11.RELEASE
的堆栈跟踪:
2018-04-06 16:27:52.512 INFO 6792 --- [主要] j.LocalContainerEntityManagerFactoryBean:构建JPA容器 EntityManagerFactory用于持久性单元'默认' 2018年4月6日 16:27:52.526 INFO 6792 --- [主要] o.hibernate.jpa.internal.util.LogHelper:HHH000204:处理 PersistenceUnitInfo [name:default ...] 2018-04-06 16:27:52.587 INFO 6792 --- [主要] org.hibernate.Version
:HHH000412:Hibernate Core {5.0.12.Final} 2018-04-06 16:27:52.589 INFO 6792 --- [main] org.hibernate.cfg.Environment
:HHH000206:未找到hibernate.properties 2018-04-06 16:27:52.591 INFO 6792 --- [main] org.hibernate.cfg.Environment
:HHH000021:字节码提供程序名称:javassist 2018-04-06 16:27:52.735 INFO 6792 --- [主要] o.hibernate.annotations.common.Version:HCANN000001:Hibernate Commons Annotations {5.0.1.Final} 2018-04-06 16:27:52.833 INFO 6792 --- [main] org.hibernate.dialect.Dialect:HHH000400:使用方言:org.hibernate.dialect.MySQL5Dialect 2018-04-06 16:27:53.180 INFO 6792 --- [主要] org.hibernate.tool.hbm2ddl.SchemaExport:HHH000227:运行hbm2ddl schema export Hibernate:drop table if exists person Hibernate:create table person(id integer not null auto_increment,name varchar(255), 主键(id))2018-04-06 16:27:53.619 INFO 6792 --- [
main] org.hibernate.tool.hbm2ddl.SchemaExport:HHH000230:Schema export complete 2018-04-06 16:27:53.646 INFO 6792 --- [
main] j.LocalContainerEntityManagerFactoryBean:初始化的JPA EntityManagerFactory用于持久性单元'默认' SessionFactory的 创建于2018-04-06 16:27:54.528 WARN 6792 --- [主要] org.hibernate.orm.connections:HHH10001002:正在使用 Hibernate内置连接池(不供生产使用!) 2018-04-06 16:27:54.529 INFO 6792 --- [主要] org.hibernate.orm.connections:HHH10001005:使用驱动程序 URL [jdbc:mysql:// localhost:3306 / hotel]的[com.mysql.jdbc.Driver] 2018-04-06 16:27:54.529 INFO 6792 --- [主要] org.hibernate.orm.connections:HHH10001001:连接 properties:{user = root,password = ****} 2018-04-06 16:27:54.529 INFO 6792 --- [main] org.hibernate.orm.connections: HHH10001003:自动提交模式:false 2018-04-06 16:27:54.532 INFO 6792 --- [main] .c.i.DriverManagerConnectionProviderImpl:HHH000115:Hibernate连接池大小:10(min = 1)2018-04-06 16:27:54.539 INFO 6792 --- [主要] org.hibernate.dialect.Dialect:HHH000400:使用方言: org.hibernate.dialect.MySQL5Dialect 2018-04-06 16:27:54.855 INFO 6792 --- [main] o.s.j.e.a.AnnotationMBeanExporter:在启动时为JMX暴露注册bean Hibernate:insert into 人(姓名)值(?)已保存!! 2018-04-06 16:27:54.954 INFO 6792 --- [main] com.hib.Hibernate5Application:在4.051秒内启动Hibernate5Application(JVM运行4.802)
以下是SpringBoot 2.0.0.RELEASE
的堆栈跟踪:
SessionFactory创建2018-04-06 16:24:17.011 INFO 5092 --- [
main] org.hibernate.Version:HHH000412:Hibernate 核心{5.2.14.Final} 2018-04-06 16:24:17.013 INFO 5092 --- [
main] org.hibernate.cfg.Environment:HHH000206: hibernate.properties not found 2018-04-06 16:24:17.915 INFO 5092 --- [主要] o.hibernate.annotations.common.Version: HCANN000001:Hibernate Commons Annotations {5.0.1.Final} 2018-04-06 16:24:17.989警告5092 --- [主要] org.hibernate.orm.connections.pooling:HHH10001002:正在使用 Hibernate内置连接池(不供生产使用!) 2018-04-06 16:24:17.994 INFO 5092 --- [主要] org.hibernate.orm.connections.pooling:HHH10001005:使用驱动程序 URL [jdbc:mysql:// localhost:3306 / hotel]的[com.mysql.jdbc.Driver] 2018-04-06 16:24:17.994 INFO 5092 --- [主要] org.hibernate.orm.connections.pooling:HHH10001001:连接 properties:{user = root,password = ****} 2018-04-06 16:24:17.994 INFO 5092 --- [main] org.hibernate.orm.connections.pooling: HHH10001003:自动提交模式:false 2018-04-06 16:24:17.996 INFO 5092 --- [main] .c.i.DriverManagerConnectionProviderImpl:HHH000115:Hibernate连接池大小:10(min = 1)2018-04-06 16:24:18.230 INFO 5092 --- [主要] org.hibernate.dialect.Dialect:HHH000400:使用方言: org.hibernate.dialect.MySQL5Dialect 2018-04-06 16:24:19.041 INFO 5092 --- [主要] com.zaxxer.hikari.HikariDataSource:HikariPool-1 - 开始...... 2018-04-06 16:24:19.058 INFO 5092 --- [
main] com.zaxxer.hikari.HikariDataSource:HikariPool-1 - 开始 完成。2018-04-06 16:24:19.419 INFO 5092 --- [主要] o.s.j.e.a.AnnotationMBeanExporter:为JMX注册bean 启动曝光2018-04-06 16:24:19.420 INFO 5092 --- [
main] o.s.j.e.a.AnnotationMBeanExporter:名称为Bean的Bean '数据源'已被自动检测为JMX曝光2018-04-06 16:24:19.427 INFO 5092 --- [主要] o.s.j.e.a.AnnotationMBeanExporter:位于MBean' dataSource': 注册JMX服务器作为MBean [com.zaxxer.hikari:name = dataSource,type = HikariDataSource] 2018-04-06 16:24:19.441 INFO 5092 --- [主要] com.hib.Hibernate5Application:已开始 Hibernate5Application在3.742秒内(JVM运行4.541) Hibernate:插入到人(名称)值(?)已保存!!
Hibernate 5
配置:
@Configuration
public class HinernateConfig {
private StandardServiceRegistry standardServiceRegistry;
private SessionFactory sessionFactory;
@Bean
public SessionFactory sessionFactory() {
try {
standardServiceRegistry = new StandardServiceRegistryBuilder().configure().build();
Metadata metadata = new MetadataSources(standardServiceRegistry).getMetadataBuilder().build();
sessionFactory = metadata.getSessionFactoryBuilder().build();
return sessionFactory;
} catch (Exception e) {
System.out.println("Exception occured while creating SessionFactory");
e.printStackTrace();
if (standardServiceRegistry != null) {
StandardServiceRegistryBuilder.destroy(standardServiceRegistry);
}
}
return sessionFactory;
}
@PostConstruct
public void post() {
System.out.println("SessionFactory Created");
}
}
主要应用:
@SpringBootApplication
public class Application implements CommandLineRunner {
private SessionFactory sessionFactory;
public Application(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
Session session = sessionFactory.openSession();
session.beginTransaction();
Person person = new Person("Mehraj Malik");
session.save(person);
System.out.println("Saved!!");
session.getTransaction().commit();
session.close();
}
}
Hibernate Xml config
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hotel</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<mapping class="com.hib.entity.Person"/>
</session-factory>
</hibernate-configuration>
application.properties
spring.datasource.url = jdbc:mysql://localhost:3306/Hotel
spring.datasource.username=root
spring.datasource.password=admin
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
在Boot 2.0堆栈跟踪中,不会出现以下部分:
运行hbm2ddl schema导出Hibernate:drop table if exists person Hibernate:创建表人(id integer not null auto_increment, name varchar(255),primary key(id))
答案 0 :(得分:0)
正如 M.Deinum 所提到的,我删除了以下类和资源文件并且它有效:
主要应用程序中的更改
@SpringBootApplication
public class Hibernate5Application implements CommandLineRunner {
private EntityManagerFactory managerFactory;
public Hibernate5Application(EntityManagerFactory managerFactory) {
this.managerFactory = managerFactory;
}
public static void main(String[] args) {
SpringApplication.run(Hibernate5Application.class, args);
}
@Override
@Transactional
public void run(String... args) throws Exception {
EntityManager entityManager = managerFactory.createEntityManager();
entityManager.getTransaction().begin();
Person person = new Person("Mehraj Malik");
entityManager.persist(person);
System.out.println("Saved!!");
entityManager.close();
}
}
我已更新my github repo