0

我仍在使用 Play Framework 学习 Ebean ORM。Play!Framework 生成的意外进化脚本有问题。我正在使用Play!Framework 2.1.1和 JDK 1.7 更新 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约束应该按照属性(1,2,3,4)的定义签入,而不是签入。我怀疑进化是通过使用我的枚举值生成的。CourseType.value(0,1,2,3)ORDINAL

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

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

亲切的问候。

4

2 回答 2

1

用户 @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;
        }
    }
于 2013-05-08T02:19:16.337 回答
0

对于第 1 个问题,我使用了来自@publiclass1的建议。


对于第 2 个问题,我了解了复合主键。在CourseInterest模型上,我使用了复合主键,因为我希望它有两种类型的主键,一种是外键(course_code),另一种是公共字段(interest_type)。所以,我尝试如下。

这是CourseInterest模型样本:

@EmbeddedId // using compound primarykey
public CourseInterestPK key;

@MapsId("courseCode") // map embedded key
@ManyToOne
@JoinColumn(name = "course_code", referencedColumnName = "code")
public Course course;

@MapsId("interestType") // map embedded key
@Constraints.Required
public InterestType interest_type;

这是CourseInterestPK(复合主键定义)类的示例:

@Embeddable
public class CourseInterest15541120PK {
     @Column(name = "course_code")
     public String courseCode;

     @Column(name = "interest_type")
     public CourseInterest.InterestType interestType;

     @Override
     public boolean equals(Object obj) {
         ... // MUST to override this method
     }

     @Override
     public int hashCode() {
         ... // MUST to override this method  
     }
}

所以,通过这些技术,我得到了我想要的进化脚本。;)

于 2013-05-08T07:54:08.187 回答