java - 具有枚举字段类型的 Ebean 模型意外生成的演化脚本

标签 java enums playframework ebean playframework-evolutions

我仍在学习使用 Play Framework 的 Ebean ORM。 Play!Framework 生成的意外进化脚本存在问题。我正在使用 Play!Framework 2.1.1 和 JDK 1.7 update 5 64 位。抱歉,这个问题的代码片段很长。

我有两个 Ebean 模型,如下所示:

Course.java

package models;

import play.data.validation.Constraints;
import play.db.ebean.Model;
import javax.persistence.*;

@Entity
@Table(name = "castillo_courses")
public class Course extends Model {
    public enum CourseType {
        COMPULSORY(1), BASIC_INTEREST(2), ADVANCED_INTEREST(3), THESIS(4);

        private int value;

        CourseType(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }
    }

    @Id
    private String code;
    @Constraints.Required
    private String course_name;
    @Constraints.Required
    private String credits;
    @Constraints.Required
    private CourseType course_type;

    // Ebean finder and Other getter and setter method
    ......
}

CourseInterest.java

package models;

import play.data.validation.Constraints;
import play.db.ebean.Model;

import javax.persistence.*;

@Entity
@Table(name = "castillo_course_interest")
public class CourseInterest extends Model {
    public enum InterestType {
        ARCHITECTURAL_INFRA(1), SOFTWARE_TECH(2), INFORMATION_PROCESSING(3), ENTERPRISE_SYSTEM(4), COMP_INTELLIGENCE(5);
        private int value;

        InterestType(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }
    }

    @Id
    @ManyToOne
    @JoinColumn(name = "course_code", referencedColumnName = "code")
    private Course course;
    @Id
    @Constraints.Required
    private InterestType interest_type;

    // Ebean finder and Other getter and setter method
    ......
}

这是从上面的模型生成的进化脚本:

# --- Created by Ebean DDL
# To stop Ebean DDL generation, remove this comment and start using Evolutions

# --- !Ups

create table castillo_courses (
  code                      varchar(255) not null,
  course_name               varchar(255),
  credits                   varchar(255),
  course_type               integer,
  constraint ck_castillo_courses_course_type check (course_type in (0,1,2,3)),
  constraint pk_castillo_courses primary key (code))
;

create table castillo_course_interest (
  course_name               varchar(255),
  credits                   varchar(255),
  course_type               integer,
  interest_type             integer not null,
  constraint ck_castillo_course_interest_course_type check (course_type in (0,1,2,3)),
  constraint ck_castillo_course_interest_interest_type check (interest_type in (0,1,2,3,4)))
;

create sequence castillo_courses_seq;

create sequence castillo_course_interest_seq;

# ..... !DOWNS code not shown

我对生成的进化脚本的期望是:

  1. castillo_courses CREATE TABLE 脚本中,ck_castillo_courses_course_type 约束应 checkin (1,2,3,4) CourseType.value 属性定义, checkin (0,1,2,3)。我怀疑进化是通过使用我的枚举的 ORDINAL 值生成此检查的。

  2. castillo_course_interest CREATE TABLE 脚本中,它再次定义了除 code 之外的所有 castillo_courses 字段。我希望脚本定义由@JoinColumn注释定义的course_code列。这里还有另一个问题。它也没有生成主键约束的脚本,因为我在模型中定义了两个@Id

我感谢任何可以解释、提供建议或帮助我解决这个问题的人。:)

谨致问候。

最佳答案

使用@EnumValue("1")

样本。

如果所有值都可以解析为整数,那么 Ebean 将持久保存并以整数而不是字符串的形式获取它们。

public enum InterestType {
       @EnumValue("1")
        ARCHITECTURAL_INFRA(1),
 @EnumValue("2")
SOFTWARE_TECH(2),
@EnumValue("3")
INFORMATION_PROCESSING(3),
 @EnumValue("4")
ENTERPRISE_SYSTEM(4),
@EnumValue("5")
 COMP_INTELLIGENCE(5);
        private int value;
        InterestType(int value) {
            this.value = value;
        }
        public int getValue() {
            return value;
        }
    }

关于java - 具有枚举字段类型的 Ebean 模型意外生成的演化脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15541120/

相关文章:

java - 可以在jtable的某些列中显示pdf作为下载链接吗?

java - 错误-日期在Jparepository中不起作用

c# - 将枚举序列化为字符串时,是否可以添加新的枚举成员而不破坏与 Newtonsoft json 的契约(Contract)?

playframework - 在play框架中连接本地h2数据库

java - 我有更新电影的更新方法。但是当我在 postman 中提供数据时,我输入一个字段,然后其余字段获取空值

java - CSS 到游戏框架中的 conf/messages

具有多个带空格参数的 Java ProcessBuilder

java - 在 java 中创建一个稀疏的 BufferedImage

Java 顶级枚举 - 父类(super class)型

c# - 到处查找枚举转换为字符串