Play Framework Ebean模型在save()-ing时抛出错误(RuntimeException)

时间:2015-05-17 11:26:38

标签: java playframework ebean

我正在寻找连续两天的答案,我似乎无法找到答案。所以我使用Play Framework时有以下两个独立的Ebean模型。

@Entity
public class User extends Model {

    public static final AtomicInteger seed = new AtomicInteger();

    @Id private final int id;

    private String name;
    private String surname;
    private String email;
    private String city;
    private String country;
    private String webpage;
    private String profileImgURL;
    private String rdfformat;
    @OneToMany(cascade = CascadeType.ALL)
    public List<Language> languages;
    @OneToMany(cascade = CascadeType.ALL)
    public List<Experience> experience;
    @OneToMany(cascade = CascadeType.ALL)
    public List<Diploma> education;
    @OneToMany(cascade = CascadeType.ALL)
    public List<Skill> skills;
    @OneToMany(cascade = CascadeType.ALL)
    public List<FriendRequest> friendRequests;
    @OneToMany(cascade = CascadeType.ALL)
    public List<Friend> friends;
    @OneToMany(cascade = CascadeType.ALL)
    public List<Recommendation> recommendations;

    public User(String name, String surname, String email, String city, String country,
        String webpage, String profileImgURL) {
        this.id = seed.incrementAndGet();
        this.name = name;
        this.surname = surname;
        this.email = email;
        this.city = city;
        this.country = country;
        this.webpage = webpage;
        this.profileImgURL = profileImgURL;
        this.languages = new ArrayList<>();
        this.experience = new ArrayList<>();
        this.education = new ArrayList<>();
        this.skills = new ArrayList<>();
        this.friendRequests = new ArrayList<>();
        this.friends = new ArrayList<>();
        this.recommendations = new ArrayList<>();
        this.rdfformat = "test";
    }

   public User() {
        this.id = seed.incrementAndGet();
        this.friends = new ArrayList<>();
        this.languages = new ArrayList<>();
        this.experience = new ArrayList<>();
        this.education = new ArrayList<>();
        this.skills = new ArrayList<>();
        this.friendRequests = new ArrayList<>();
        this.friends = new ArrayList<>();
        this.recommendations = new ArrayList<>();
   }

  public int getId() { return id; }
  public com.hp.hpl.jena.rdf.model.Model getModel() {return model;}
  public String getRDFModel() {return rdfformat;}
  public String getName() { return name; }
  public String getSurname() { return surname; }
  public String getEmail() { return email; }
  public String getCity() { return city; }
  public String getCountry() { return country; }
  public String getWebpage() { return webpage; }
  @JsonProperty("profile_img_url")
  public String getProfileImgURL() { return profileImgURL; }
  public List<Language> getLanguages() { return languages; }
  public List<Experience> getExperience() { return experience; }
  public List<Diploma> getEducation() { return education; }
  public List<Skill> getSkills() { return skills; }
  public List<FriendRequest> getFriendRequests() { return friendRequests; }
  public List<Friend> getFriends() { return friends; }
  public List<Recommendation> getRecommendations() { return recommendations; }

  public void setName(String name) { this.name = name; }
  public void setSurname(String surname) { this.surname = surname; }
  public void setEmail(String email) { this.email = email; }
  public void setCity(String city) { this.city = city; }
  public void setCountry(String country) { this.country = country; }
  public void setWebpage(String webpage) { this.webpage = webpage; }
  @JsonProperty("profile_img_url")
  public void setProfileImgURL(String profileImgURL) { this.profileImgURL = profileImgURL; }
  public void setLanguages(List<Language> languages) { this.languages = languages; }
  public void setExperience(List<Experience> experience) { this.experience = experience; }
  public void setEducation(List<Diploma> education) {   this.education = education; }
  public void setSkills(List<Skill> skills) { this.skills = skills; }
  public void setFriendRequests(List<FriendRequest> friendRequests) { this.friendRequests = friendRequests; }
  public void setFriends(List<Friend> friends) { this.friends = friends; }
  public void setRecommendations(List<Recommendation> recommendations) { this.recommendations = recommendations; }
}

@Entity
public class Vacancy extends Model {

    public static final AtomicInteger seed = new AtomicInteger();

    @Id private final int vacId;

    private int authorId;
    private String title;
    private String location;
    private int months;
    private String salary;
    private String description;
    @OneToMany(cascade = CascadeType.ALL)
    public List<Diploma> requiredDiplomas;
    @OneToMany(cascade = CascadeType.ALL)
    public List<Experience> requiredExperience;
    @OneToMany(cascade = CascadeType.ALL)
    public List<Skill> requiredSkills;
    @OneToMany(cascade = CascadeType.ALL)
    public List<Language> requiredLanguages;

    public Vacancy(int userId, String title, String location, int months, String salary, String description) {
        this.vacId = seed.incrementAndGet();
        this.authorId = userId;
        this.title = title;
        this.location = location;
        this.months = months;
        this.salary = salary;
        this.description = description;
        this.requiredDiplomas = new ArrayList<>();
        this.requiredExperience = new ArrayList<>();
        this.requiredSkills = new ArrayList<>();
        this.requiredLanguages = new ArrayList<>();
    }

    public Vacancy() {
        this.vacId = seed.incrementAndGet();
        this.requiredDiplomas = new ArrayList<>();
        this.requiredExperience = new ArrayList<>();
        this.requiredSkills = new ArrayList<>();
        this.requiredLanguages = new ArrayList<>();
    }

    public int getVacId() { return vacId; }
    public int getAuthorId() { return authorId; }
    public String getTitle() { return title; }
    public String getLocation() { return location; }
    public int getMonths() { return months; }
    public String getSalary() { return salary; }
    public String getDescription() { return description; }
    public List<Diploma> getRequiredDiplomas() { return requiredDiplomas; }
    public List<Experience> getRequiredExperience() { return requiredExperience; }
    public List<Skill> getRequiredSkills() { return requiredSkills; }
    public List<Language> getRequiredLanguages() { return requiredLanguages; }

    public void setAuthorId(int userId) { this.authorId = userId; }
    public void setTitle(String title) { this.title = title; }
    public void setLocation(String location) { this.location = location; }
    public void setMonths(int months) { this.months = months; }
    public void setSalary(String salary) { this.salary = salary; }
    public void setDescription(String description) { this.description = description; }
    public void setRequiredDiplomas(List<Diploma> requiredDiplomas) { this.requiredDiplomas = requiredDiplomas; }
    public void setRequiredExperience(List<Experience> requiredExperience) { this.requiredExperience = requiredExperience; }
    public void setRequiredSkills(List<Skill> requiredSkills) { this.requiredSkills = requiredSkills; }
    public void setRequiredLanguages(List<Language> requiredLanguages) { this.requiredLanguages = requiredLanguages; }
}

两个模型都有一个@Id,并且有自己的写入getter。现在在我的应用程序控制器中,我有一个名为populate()的函数,它清理数据库并用数据填充它。功能如下:

public static Result populate() throws ParseException {

    cleanup();
    DateFormat df = new SimpleDateFormat("dd-MM-yyyy");

    // USER 2
    User user2 = new User("John", "Doe", "john.doe@gmoil.com", "London", "United Kingdom", "www.johndoe.com", "");
    user2.addLanguage(new Language("english",4));
    user2.save();

    // VACANCY 1
    Vacancy vacancy1 = new Vacancy(5, "Software Developer", "London, UK", 24, "60-80K", "Program things");
    vacancy1.addRequiredDiploma(new Diploma("Bachelor's degree", "Computer Science"));
    vacancy1.addRequiredExperience(new Experience("Software Developer", 3));
    vacancy1.addRequiredSkill(new Skill("Java", 4));
    vacancy1.addRequiredLanguage(new Language("english", 4));
    vacancy1.save(); // ERROR HERE

    return ok("Users added! (" + User.find().findRowCount() + ")\nVacancies added! (" + Vacancy.find().findRowCount() + ")\n");
}

现在当我尝试调用populate时,我在vacacancy1.save()行上收到以下错误消息:

 [RuntimeException: get id on [models.User] type[models.Vacancy] threw error.]

请记住,当只有一个模型(用户)时,一切都能正常运行。根据我的理解,它与模型的id有关,但我无法弄清楚是什么。我已经尝试过几个方面,比如重命名它们,给它们命名相同,为OneToMany模型中的id提供getter。有人有想法吗?提前谢谢。

EDIT1:我弄清楚导致错误的原因,但我还没有找到解决方案。这主要是因为我在用户和空缺中使用了一些模型(列表),因此我猜这些模型时,它不知道它属于哪个(用户或空缺)?我不确定,但是当我从Vacancy中删除列表时错误消失了。

0 个答案:

没有答案