示例基本模型如下所示:
package models.db;
import play.db.ebean.Model;
import javax.persistence.*;
import java.util.Date;
@Entity
@Table(name = "content")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "dtype", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("content")
public abstract class Content extends Model {
@Id
public Long id;
@Column(name = "dtype", insertable = false, updatable = false)
public String dtype;
public static Finder<Long, Content> find = new Finder<>(Long.class, Content.class);
public String title;
public Date created = new Date();
public Date modified = new Date();
}
然后你可以像这样扩展它:
package models.db;
import javax.persistence.*;
@Entity
@DiscriminatorValue("news")
public class News extends Content {
@Id
public Long id;
public static Finder<Long, News> find = new Finder<>(Long.class, News.class);
public String newsSource;
}
or
package models.db;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.Date;
@Entity
@DiscriminatorValue("post")
public class Post extends Content {
@Id
public Long id;
public static Finder<Long, Post> find = new Finder<>(Long.class, Post.class);
public Date publishDate;
}
因此您可以通过以下方式选择所有内容:
List<Content> contents = Content.find.where().orderBy("dtype ASC").findList();
当然,这些对象只有共享字段:id
, dtype
, title
, created
and modified
,用于获取即(新闻)newsSource
或(邮寄)publishDate
您需要使用自己的查找器来获取这些对象,即使用id
价值来自general内容查询。