【问题标题】:How to search in h2 database Play Framework 2.7.3?如何在 h2 数据库 Play Framework 2.7.3 中搜索?
【发布时间】:2020-01-30 21:57:02
【问题描述】:

我需要在 h2 数据库中搜索具有特定 codeCompany 类实例,但我不知道如何。

我尝试过使用 Finder 类,但在我使用的版本中似乎没有任何查找方法,除了 findbyid()。 这是我的公司课程的开始:

@Entity
public class Company extends Model {
    @Id
    public Integer id;
    public String code;
    public String name;
    public String adress;
    public String fiscalCode;
    public String bankAccount;

    public static Finder<Integer, Company> find = new Finder<>(Company.class);

谢谢!

【问题讨论】:

    标签: java playframework ebean


    【解决方案1】:

    根据您使用的 Play 版本,我建议您尝试以下方法:

    Company.find.where().like("code", "%foo%").findList();

    1. 定义您的查找器
    public class CompanyFinder extends Finder<Long,Company> {
    
      public CompanyFinder() {
        super(Company.class);
      }
    
      // Add finder methods ...
    
      public Company byCode(String code) {
        return query().eq("code", code).findOne();
      }
    }
    
    1. 更新您的实体以引用此查找器:
    @Entity
    public class Company extends BaseModel {
    
      public static final CompanyFinder find = new CompanyFinder();
      ...
    }
    
    1. 呼叫发现者:

    Company c = Company.find.byCode("foo");

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 2023-04-02
      • 2015-04-25
      • 2016-12-12
      • 2019-12-07
      • 2011-09-30
      相关资源
      最近更新 更多