如何在Play框架上使用Ebean创建可连接实体

时间:2014-03-06 19:46:01

标签: join playframework left-join ebean

如何在Ebean中创建自动引用其他表格的enteties?

例如在这种情况下:

  • 用户有很多系列照片。
  • 照片系列有很多照片。
  • 照片有扩展名。
  • 扩展程序没有引用。

代码:

@Entity
    public class User extends Model {
    @Id
    public int id;

    @OneToMany
    public List<PhotoSerie> photoSeries;

    //XXXX
}

@Entity
public class PhotoSerie extends Model {

    @Id
    public int id;

    @OneToMany
    public List<Photo> photo;

    //XXXX
}

@Entity
public class Photo extends Model {

    @Id
    public int id;

    @OneToOne
    PhotoExtension photoExtension;

    //XXXX
}

@Entity
public class PhotoExtension extends Model {

    @Id
    public int id;

    public String extension;

    //XXXX
}

将生成以下错误:

[error] c.a.e.s.d.BeanDescriptorManager - Error in deployment
javax.persistence.PersistenceException: Error on models.PhotoSerie.photo. @OneTo
Many MUST have Cascade.PERSIST or Cascade.ALL bejavax.persistence.PersistenceExc
eption: Error on models.PhotoSerie.photo. @OneToMany MUST have Cascade.PERSIST o
r Cascade.ALL because this is a unidirectional relationship. That is, there is n
o property of type class models.PhotoSerie on class models.Photocause this is a
unidirectional relationship. That is, there is no property of type class models.
PhotoSerie on class models.Photo

如何创建以正确方式与其他表连接的实体?

1 个答案:

答案 0 :(得分:1)

您需要在OneToMany注释上使用mappedBy,并在关系所有者上使用CascadeType.ALL。 EBean与常规ORM略有不同,在play文档中,他们提到使用setter / getter而不是字段引用。您还需要一些查找器来进行查找。因此,使用setter / getter方法,使用play 2.1.2(至少我测试过),这有效:

用户:

package models.test;

import play.db.ebean.Model;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
public class User extends Model {
    @Id
    public int id;

    public String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    public List<PhotoSeries> photoSeries;

    public static Finder<String, User> find = new Finder<String, User>(
            String.class, User.class);

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public List<PhotoSeries> getPhotoSeries() {
        return photoSeries;
    }

    public void setPhotoSeries(List<PhotoSeries> photoSeries) {
        this.photoSeries = photoSeries;
    }
}

PhotoSeries:

package models.test;

import play.db.ebean.Model;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
public class PhotoSeries extends Model {

    @Id
    public int id;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "photoSeries")
    public List<Photo> photoList;

    @ManyToOne
    public User user;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public List<Photo> getPhotoList() {
        return photoList;
    }

    public void setPhotoList(List<Photo> photoList) {
        this.photoList = photoList;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

PhotoExtension:

package models.test;


import play.db.ebean.Model;

import javax.persistence.Entity;
import javax.persistence.Id;


@Entity
public class PhotoExtension extends Model {

    @Id
    public int id;

    public String extension;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getExtension() {
        return extension;
    }

    public void setExtension(String extension) {
        this.extension = extension;
    }
}

照片:

package models.test;

import play.db.ebean.Model;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;


@Entity
public class Photo extends Model {

    @Id
    public int id;

    @OneToOne
    PhotoExtension photoExtension;

    @ManyToOne
    PhotoSeries photoSeries;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public PhotoExtension getPhotoExtension() {
        return photoExtension;
    }

    public void setPhotoExtension(PhotoExtension photoExtension) {
        this.photoExtension = photoExtension;
    }

    public PhotoSeries getPhotoSeries() {
        return photoSeries;
    }

    public void setPhotoSeries(PhotoSeries photoSeries) {
        this.photoSeries = photoSeries;
    }
}


    @Test
    public void testPhotoSeries() {
        User user = new User();
        user.setName("Bob");
        List<PhotoSeries> photoSeriesList = new ArrayList<PhotoSeries>();
        PhotoSeries photoSeries = new PhotoSeries();
        photoSeries.setUser(user);
        photoSeriesList.add(photoSeries);
        user.setPhotoSeries(photoSeriesList);
        user.save();


        user = User.find.where().eq("name", "Bob").findUnique();
        log.info("user has " + user.getPhotoSeries().size() + " photo series");

        List<Photo> photoList = new ArrayList<Photo>();
        Photo photo = new Photo();
        photoList.add(photo);

        photoSeries = user.getPhotoSeries().get(0);
        photoSeries.setPhotoList(photoList);

        user.update();
        user = User.find.where().eq("name", "Bob").findUnique();

        photoSeries = user.getPhotoSeries().get(0);
        photo = photoSeries.getPhotoList().get(0);

        log.info("photo " + photo);
    }