首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >和PostgreSQL自动生成Enum类型

和PostgreSQL自动生成Enum类型
EN

Stack Overflow用户
提问于 2019-02-20 17:11:52
回答 2查看 2.5K关注 0票数 0

如果使用MySQL,则可以成功地创建实体和枚举。

我将数据库更改为PostgreSQL9.4。因此,我有一些错误如下:

..。导致: org.hibernate.tool.schema.spi.SchemaManagementException:无法对JDBC目标创建表角色执行模式管理(id int8 not null,name枚举(‘int8’,'USER','SEARCH')不为空,主键(Id) org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:59) ~hibernate的-核-5.0.12.final.jar:5.0.12在org.hibernate.tool.schema.internal.SchemaMigratorImpl.applySqlString(SchemaMigratorImpl.java:431) ~hibernate-核心-5.0.12.Final.jar:5.0.12在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.jar在org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigrationToTargets(SchemaMigratorImpl.java:167) ~hibernate-core-5.0.12.Final.jar:5.0.12在org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigration(SchemaMigratorImpl org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455) ~PostgreSQL9.4.1212.jre7.jar:9.4.1212.jre7位于org.postgresql.core.v3.QueryExecutorImpl。执行( org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:430) )~PostgreSQL9.4.1212.jre7.jar:9.4.1212.jre7 at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:356) ~PostgreSQL9.4.1212.jre7.jar:9.4.1212.jre7.jar:9.4.1212.jre7.jar在org.postgresql.jdbc.PgStatement.execute(PgStatement.java:356)~PostgreSQL9.4.1212.jre7.jar:9.4.1212.jre7.jar( 303) ~PostgreSQL9.4.1212.jre7.jar:9.4.1212.jre7在org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:289) ~PostgreSQL9.4.1212.jre7.jar:9.4.1212.jre7.jar:9.4.1212.jre7在org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:266) ~PostgreSQL9.4.1212.jre7.jar: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(Native方法) ~na:1.8.0_191 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~na:1.8.0_191 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~na:1.8.0_191 at java.lang.reflect.Method.invoke(Method.java:498) ~na:1.8.0_191 at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114) ~tomcat-jdbc-8.5.14.jar:na,地址为com.sun.agent。$Proxy93.ExecuteUpdate(未知源) ~na:na在org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:56) ~hibernate-核-5.0.12.Final.jar:5.0.12最后.32个常见帧省略了

我的灵魂:

代码语言:javascript
复制
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;
    }
}

我的角色课:

代码语言:javascript
复制
@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文件:

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
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

代码语言:javascript
复制
<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>

如何将其配置为自动生成枚举类型?

EN

回答 2

Stack Overflow用户

发布于 2019-02-20 17:25:44

我假设您希望从MySQL迁移到PostgreSQL。

使用DB枚举PostgreSQL不同于MySQL。

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

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

票数 1
EN

Stack Overflow用户

发布于 2019-02-20 20:49:57

“枚举(.)”用于mysql。我把电话线移开了,问题就解决了。

@列(columnDefinition= "enum('ADMIN','USER','SEARCH')")

然而,开源 hibernate-type项目允许您映射特定于数据库的列.我们将从这里这里中了解如何在使用JPA和Hibernate时将PostgreSQL Enum类型映射到PostgreSQL数组。但我不知道如何在弹簧靴上使用这个。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54791884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档