Pergunta

I still learning about Ebean ORM with Play Framework. have problem with unexpected evolution script that generated by Play!Framework. I'm using Play!Framework 2.1.1 with JDK 1.7 update 5 64-bit. Sorry, for long code snippet in this question.

I have two Ebean Model looks like following:

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
    ......
}

This is generated evolution script from the models above:

# --- 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

What I expected with the generated evolution script is:

  1. In castillo_courses CREATE TABLE script, ck_castillo_courses_course_type constraint should check in (1,2,3,4) as defined by CourseType.value attribute, not to check in (0,1,2,3). I suspect evolution generated this check by using ORDINAL value of my Enums.

  2. In castillo_course_interest CREATE TABLE script, it define again all castillo_courses fields except code. I expect the script is only define course_code column as defined by @JoinColumn annotation. There is another problem here. It has no script to generate primary key constraint too, because I have defined two @Id defined in model.

I appreciate to anyone that can explain, give advice, or help me with this problem.. :)

Kindly regards.

Foi útil?

Solução 2

For question number 1, I used suggestion from @publiclass1.


For question number 2, I learn about Compound Primary Key. On CourseInterest model, I used the Compound Primary Key because I want it to have 2 type of primary key, one is the foreign key (course_code) and the other is a common field (interest_type). So, I tried like following.

This is sample of CourseInterest model :

@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;

This is sample of CourseInterestPK (Compound Primary Key definition) class:

@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  
     }
}

So, with these technique, I get the evolution script that I want to. ;)

Outras dicas

user the @EnumValue("1")

sample.

If all the values are parsable as Integers then Ebean will persist and fetch them as integers rather than strings.

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;
        }
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top