我仍在学习使用 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
我对生成的进化脚本的期望是:
在
castillo_courses
CREATE TABLE
脚本中,ck_castillo_courses_course_type
约束应 checkin(1,2,3,4)
由CourseType.value
属性定义,不 checkin(0,1,2,3)
。我怀疑进化是通过使用我的枚举的ORDINAL
值生成此检查的。在
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/