播放框架:继承按类型排序 [英] Play Framework: Inheritance sort by type
问题描述
在我的应用程序中,我有2个类:
In my Application, I have 2 Classes:
- Group
- Model
和一个基类Element
.
我使用单表策略来保留这些模型. (strategy = InheritanceType.SINGLE_TABLE)
.因此,在我的表格中创建了列dtype
.
I use the single table strategy to persist these models. (strategy = InheritanceType.SINGLE_TABLE)
. Thus a column dtype
is created in my table.
我现在正尝试根据此类型对页面进行排序:
I'm now trying to sort my pages based on this type:
find.where().disjunction()
.add(Expr.ilike("name", "%" + filter + "%"))
.orderBy("dtype asc, name asc," + sortBy + " " + order).findList()
但这会引发异常,找不到dtype.
But this throws an Exception, that dtype cannot be found.
如何根据类型排序?
谢谢!
推荐答案
样本基本模型如下:
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();
}
然后您可以将其扩展为:
Then you can extend it like:
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;
}
因此,您可以通过以下方式选择所有内容:
So you can choose all contents via:
List<Content> contents = Content.find.where().orderBy("dtype ASC").findList();
当然,这些对象将只有共享字段:id
,dtype
,title
,created
和modified
,用于获取(新闻)newsSource
或(发布)publishDate
您需要使用自己的查找器来获取这些对象,即使用 general 内容查询中的id
值.
Of course these objects will have only shared fields: id
, dtype
, title
, created
and modified
, for getting i.e. (News) newsSource
or (Post) publishDate
you need to get these objects with their own finders i.e. using id
value from general Content query.
这篇关于播放框架:继承按类型排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!