Search code examples
javaplayframeworkexpressionebean

Java Play Ebean - how to use Expression Expr.Or() or like that


I have a class, that use ebean in Java Play Framework. I try use Expression Expr.Or() and I want to return type is PageList<News>

@Entity
@Table(name = "news")
public class News extends Model {

@Id
@Column(name = "news_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long newsId; // ID

@Column(name = "title")
public String title; // タイトル

@Lob
@Column(name = "article")
public String article; // 記事内容

@Column(name = "view_count")
public Integer viewCount = 0; // 閲覧数

@Column(name = "category")
public Integer category; // 1:キャリア 2: スキル 3:テクノロジー

@Column(name = "state")
public Boolean state = false; // 状況 false:下書き true: 公開

@Column(name = "recommend")
public Boolean recommend = false; // おすすめ false:なし true:おすすめ

@Column(name = "created")
@CreatedTimestamp
public Date created; // 登録び

@Column(name = "modified")
@UpdatedTimestamp
public Date modified; // 更新日

@Lob
@Column(name = "image")
public String image; // 画像

@Column(name = "is_deleted")
public Boolean isDeleted = false;

}    

In Play Ebean, I want to find news like query

SELECT * FROM NEWS
WHERE title LIE '%?%' 
OR article LIKE '%?%'
OR category LIKE '%?%'
OR state = ?
OR recommend = '?
OR created = '?

So, how I can write code. I try use Expression Expr.Or() but Expression Expr.Or() but this can use only two expressions. Please help me.


Solution

  • Try nested Expr.or() statements

     Ebean.find(News.class).where().or(
        Expr.or( Expr.like("title", "%?%"), Expr.like("article", "%?%")),
        Expr.or(Expr.like("category", "%?%"),Expr.like("state","?"))
      );