Play Framework:继承按类型排序

时间:2014-10-21 21:08:25

标签: jpa playframework-2.0 ebean

在我的应用程序中,我有两个类:

- Group
- Model

和一个基类Element

我使用单表策略来保留这些模型。 (strategy = InheritanceType.SINGLE_TABLE)。因此,在我的表中创建了一列dtype

我现在正在尝试根据这种类型对页面进行排序:

find.where().disjunction()
                .add(Expr.ilike("name", "%" + filter + "%"))
                .orderBy("dtype asc, name asc," + sortBy + " " + order).findList()

但这引发了一个异常,即找不到dtype。

如何根据类型进行排序?

谢谢!

1 个答案:

答案 0 :(得分:2)

示例基本模型可能如下所示:

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;

}

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();

当然,这些对象只有共享字段:iddtypetitlecreatedmodified,用于获取ie(新闻){ {1}}或(发布)newsSource您需要使用自己的查找程序获取这些对象,即使用常规内容查询中的publishDate值。

相关问题