[英]How to traverse 2 unidirectional Ebean rels @ManyToOne and @OneToOne
Didn't find the answer around here. 在这里找不到答案。 I have 3 objects (only showing relevant parts): 我有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);
}
The rels are unidirectional, that is, Module has no reference to the other 2 entities. 相对关系是单向的,也就是说,模块没有引用其他2个实体。
Questions: 问题:
In very general: it's good idea to add a Boolean flag to the Module
model, thanks to this, you don't need to write sophisticated queries for finding modules containing relations, you can just check the flag: 一般来说,将布尔标志添加到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();
}
using the isFeatured
flag you're able to filter ModuleVersion
s as well easily: 使用isFeatured
标志,您还可以轻松过滤ModuleVersion
:
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();
}
Of course for 'automatic' setting the flag you should override save()
and update(Object o)
methods in your Module
model, let me know if you need a sample for this. 当然,对于“自动”设置标志,您应该在Module
模型中覆盖save()
和update(Object o)
方法,请告知我是否需要此示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.