Spring Boot数据JPA和PostgreSQL自动生成枚举类型

时间:2019-02-20 17:11:52

标签: java postgresql spring-boot spring-boot-jpa

如果使用MySQL,将成功创建我的实体和枚举。

我将数据库更改为PostgreSQL-9.4。因此,我有一些错误,如下所示:

  

...原因:   org.hibernate.tool.schema.spi.SchemaManagementException:无法   对JDBC目标执行模式管理[创建表角色(id int8   不为null,名称为enum('ADMIN','USER','SEARCH')不为null,主键   (id))]   org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:59)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]在   org.hibernate.tool.schema.internal.SchemaMigratorImpl.applySqlString(SchemaMigratorImpl.java:431)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]在   org.hibernate.tool.schema.internal.SchemaMigratorImpl.applySqlStrings(SchemaMigratorImpl.java:420)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]在   org.hibernate.tool.schema.internal.SchemaMigratorImpl.createTable(SchemaMigratorImpl.java:236)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]在   org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigrationToTargets(SchemaMigratorImpl.java:167)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]在   org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigration(SchemaMigratorImpl.java:60)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]在   org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:134)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]在   org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:101)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]在   org.hibernate.internal.SessionFactoryImpl。(SessionFactoryImpl.java:472)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]在   org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]在   org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879)   〜[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final] ... 22   常见的框架已省略

     

原因:org.postgresql.util.PSQLException:   错误:类型“枚举”不存在位置:43,位于   org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455)   〜[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]在   org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2155)   〜[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]在   org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:288)   〜[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]在   org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:430)   〜[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]在   org.postgresql.jdbc.PgStatement.execute(PgStatement.java:356)   〜[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]在   org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:303)   〜[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]在   org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:289)   〜[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]在   org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:266)   〜[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]在   org.postgresql.jdbc.PgStatement.executeUpdate(PgStatement.java:246)   〜[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]在   sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)   〜[na:1.8.0_191]在   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)   〜[na:1.8.0_191]在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)   〜[na:1.8.0_191]在java.lang.reflect.Method.invoke(Method.java:498)   〜[na:1.8.0_191]在   org.apache.tomcat.jdbc.pool.StatementFacade $ StatementProxy.invoke(StatementFacade.java:114)   〜[tomcat-jdbc-8.5.14.jar:na]在   com.sun.proxy。$ Proxy93.executeUpdate(未知来源)〜[na:na]位于   org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:56)   〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] ... 32个常见框架   省略

我的枚举:

public enum RoleType {

    ADMIN("ADMIN"),

    SEARCH("SEARCH"),

    USER("USER");

    private final String value;

    RoleType(final String value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return value;
    }
}

我的角色类别:

@Entity
@Table(name = "role")
public class Role {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @JsonIgnore
    private Long id;

    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(columnDefinition = "enum('ADMIN', 'USER', 'SEARCH')")
    private RoleType name;

    @ManyToMany(mappedBy = "roles")
    @JsonIgnore
    private Set<User> users;

    public Role() {
    }

    public Role(RoleType name) {
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public RoleType getName() {
        return name;
    }

    public void setName(RoleType name) {
        this.name = name;
    }

    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

    @Override
    public String toString() {
        return name.toString();
    }
}

我的用于postgresql的application.yml文件:

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/mydb?autoReconnect=true&useSSL=false
    username: username
    password: password
    driverClassName: org.postgresql.Driver
  jpa:
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect

我用于mysql的旧application.yml文件:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb?autoReconnect=true&useSSL=false
    username: user
    password: password
    driverClassName: com.mysql.jdbc.Driver
  jpa:
    hibernate:
      ddl-auto: update

我的pom.xml

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

        ...

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>

        <!--dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency-->

    </dependencies>

如何配置它以自动生成枚举类型?

2 个答案:

答案 0 :(得分:1)

我假设您想从MySQL迁移到PostgreSQL。

使用数据库枚举PostgreSQL与MySQL不同。

在此处了解有关如何在PostgreSQL中创建枚举的更多信息:https://www.postgresql.org/docs/9.1/datatype-enum.html

这意味着您不能使用JPA / Hibernate创建枚举。

答案 1 :(得分:0)

“ enum(...)”用于mysql。我删除了该行,问题已解决。

  

@Column(columnDefinition =“ enum('ADMIN','USER','SEARCH')”)

但是,open-source休眠类型项目使您可以映射数据库特定的列。我们将从herehere中看到如何在使用JPA和Hibernate时将PostgreSQL Enum类型映射到Java数组。但是我不确定如何在Spring Boot中使用它。