我想用spring boot和hibernate建表,虽然没有报错,但是建不了表,为啥?

Posted

技术标签:

【中文标题】我想用spring boot和hibernate建表,虽然没有报错,但是建不了表,为啥?【英文标题】:I want to use springboot and hibernate to create a table,although there is no error,but table can't be created,why?我想用spring boot和hibernate建表,虽然没有报错,但是建不了表,为什么? 【发布时间】:2017-07-23 03:07:16 【问题描述】:

这里是application.properties

server.port=8080
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false

spring.datasource.url=jdbc:mysql://127.0.0.1/springboot?characterEncoding=UTF-8&autoReconnect=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver

spring.datasource.max-active=5
spring.datasource.max-idle=2
spring.datasource.min-idle=1
spring.datasource.initial-size=1
spring.datasource.initialize=false
spring.datasource.test-on-borrow=false
spring.datasource.test-on-return=false
spring.datasource.test-while-idle=true
#spring.datasource.max-wait-millis=30000
spring.datasource.validation-query=SELECT 1
spring.datasource.time-between-eviction-runs-millis=20000
spring.datasource.min-evictable-idle-time-millis=28700

# Specify the DBMS
spring.jpa.database = MYSQL
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

这里是pom.xml,我认为是正确的。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.7.Final</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

但是当我运行它时,控制台会显示:

2017-03-03 13:16:57.999 INFO 29616 --- [main] org.hibernate.Version : HHH000412: Hibernate Core 4.3.7.Final 2017-03-03 13:16:58.001 INFO 29616 --- [main] org.hibernate.cfg.Environment:HHH000206:找不到 hibernate.properties 2017-03-03 13:16:58.003 INFO 29616 --- [main] org.hibernate.cfg.Environment:HHH000021:字节码提供程序名称:javassist

其他什么都没有。但是无法创建表。

【问题讨论】:

pom.xml中移除hibernate-core依赖,通过spring-boot-starter-data-jpa传递管理 好的,我试试看。 【参考方案1】:

spring.jpa.hibernate.ddl-auto=update 更改为 spring.jpa.hibernate.ddl-auto=create ...注意 spring.jpa.hibernate.ddl-auto 不应在 prod 中使用

【讨论】:

【参考方案2】:

我遇到了同样的问题,我通过将实际路径从 h2 控制台复制到 spring.datasource.url="&lt;paste path here&gt;" 来解决它 在application.properties

【讨论】:

以上是关于我想用spring boot和hibernate建表,虽然没有报错,但是建不了表,为啥?的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate 不使用 Spring boot 在 PostgreSql 中创建数据库

spring boot中的@Transactionnal注解真的会创建事务吗?

如何使用 Hibernate 和 Spring Boot 配置和监控 HikariCP

Spring Boot Hibernate 约束验证器

使用 Spring Boot 和 Spring Data JPA 的 Hibernate 拦截器或侦听器

如何在spring boot和hibernate中计算特定id的记录