1

このあたりで答えが見つかりませんでした。私は3つのオブジェクトを持っています(関連する部分のみを表示しています):

@Entity 
class Module {
} 

@Entity
class FeaturedModule {
    @OneToOne(optional = false)
    public Module playModule;

    public static final Finder<Long, FeaturedModule> FIND = new Finder<Long, FeaturedModule>(Long.class, FeaturedModule.class);
}

@Entity
class ModuleVersion {
    @ManyToOne
    public Module playModule

    public static final Finder<Long, ModuleVersion> FIND = new Finder<Long, ModuleVersion>(Long.class, ModuleVersion.class);
}

relは単方向です。つまり、Moduleは他の2つのエンティティを参照していません。

質問:

  1. FeaturesModulesを使用してrel内にないモジュールを(ModuleVersionから)検索する方法
  2. 特定のModuleVersionを持つ一連のFeaturedModuleを(FeaturedModulesから)検索する方法
4

1 に答える 1

1

一般的には、モデルにブールフラグを追加することをModuleお勧めします。これにより、リレーションを含むモジュールを検索するための高度なクエリを作成する必要がなくなり、フラグを確認するだけで済みます。

public static List<Module> findAllFeatured() {
    return find.select("id,name").where().eq("isFeatured", true).findList();
}

public static List<Module> findAllNotFeatured() {
    // give here list of field to select WITHOUT featured, otherwise it will 
    // create a JOIN to featured which will result that you will be not able
    // to get records without associations
    return find.select("id, name").where().eq("isFeatured", false).findList();
} 

isFeaturedフラグを使用すると、ModuleVersionsも簡単にフィルタリングできます。

public static List<ModuleVersion> findAll(String version) {
    return find.fetch("module").where().like("version", version).findList();
}

public static List<ModuleVersion> findFeatured(String version) {
    return find.fetch("module")
            .where().like("version", version).eq("module.isFeatured", true).findList();
}

public static List<ModuleVersion> findNotFeatured(String version) {
    return find.fetch("module")
            .where().like("version", version).eq("module.isFeatured", false).findList();
}

もちろん、「自動」でオーバーライドする必要のあるフラグとsave()モデルのupdate(Object o)メソッドを設定するModule場合は、このサンプルが必要かどうかをお知らせください。

于 2012-10-21T20:17:59.370 に答える