Hibernate不会自动创建表

时间:2019-06-16 14:18:13

标签: java hibernate spring-boot

pom.xml:

<dependencies>
    ...
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>4.2.2.RELEASE</version>
</dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.0.3.Final</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </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>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
</dependencies>

实体:

@Entity
@Table(name = "person")
public class Person {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int personId;

@Column(name = "first_name")
private String firstName;
}

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/socialmedia? 
allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver.class=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.default_schema = socialmedia
spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.show-sql = true

Hibernate不会自动创建表,我设置了属性和实体类,并且在运行代码后,依存关系仍然为空。没有显示错误消息。

1 个答案:

答案 0 :(得分:0)

您的策略是:

def foo2[A, B](as: Seq[A])(f: A => B): Seq[(A, B)] = {
  as map as.toSet.par.map((a: A) => a -> (a, f(a))).seq.toMap
}

这意味着Hibernate创建表,并在程序运行后删除表。这就是为什么您看不到任何表格的原因。

您必须使用create(仅创建表)或update(还更改表):

spring.jpa.hibernate.ddl-auto = create-drop

然后,我在pom.xml中看不到JPA入门项目,但是您具有不必要的Hibernate依赖关系。您的pom.xml应该如下所示:

 spring.jpa.hibernate.ddl-auto = update

spring-boot-starter-data-jpa将自动配置Hibernate。