外键查询播放!框架2. + ebean [英] foreign key query play! framework 2.+ ebean
问题描述
我是新来玩框架和Ebean ORM的人.
I'm new to play framework and the Ebean ORM.
基本上,我有两个模型,一个是RegUsers
,另一个是RegIds
.在我的注册ID中,我在字段RegUsers
上建立了多对一关系.声明,如果我没记错的话,一个用户可以有许多注册ID.
Basically, I have two models one is a RegUsers
the other is RegIds
. In my registration Ids I put a many to one relationship on the Field RegUsers
. Stating, if I'm not mistaken that one user can have many registration ids.
RegId
型号:
@Entity
public class RegId extends Model{
public static Finder<Long,RegId> finder = new Finder<Long,RegId>(Long.class, RegId.class);
@Id
public Long id;
@ManyToOne
public RegUsers regUsers;
public String regId;
}
RegUser
型号:
@Entity
public class RegUsers extends Model{
public static Finder<Long,RegUsers> find = new Finder<Long,RegUsers>(Long.class, RegUsers.class);
@Id
public Long id;
public String email;
public String name;
}
RegId
必须具有RegUser
,但是RegUser
不需要具有RegId
.
对于我的一种观点,我试图向所有仅拥有一次RegId
的RegUsers
展示,但无法弄清楚该怎么做.
A RegId
must have a RegUser
, but a RegUser
does not need to have a RegId
.
For one of my views I'm trying to show all RegUsers
who do have an RegId
only once, but cannot figure out how to do this.
我很像这样查询RegId
表:
List<RegId> reg = RegId.finder.where().findList();
然后通过以下方式查询RegUser.name:
And then querying the RegUser.name by:
@(regs: List[RegId])
@for(reg <- regs){
<p>@reg.regUsers.email</p>
}
但是我不知道如何只显示不同的记录.有没有办法使用Ebean做到这一点,或者我需要写原始sql吗?我将不胜感激任何帮助.
But I can't figure out how to only show distinct records. Is there a way to do this using Ebean or will I need to writ raw sql? I would appreciate any help.
推荐答案
如果您向RegUsers定义中添加regIds
字段,如下所示:
if you add a regIds
field to your RegUsers definition like this:
@Entity
public class RegUsers extends Model{
public static Finder<Long,RegUsers> find = new Finder<Long,RegUsers>(Long.class, RegUsers.class);
@Id
public Long id;
public String email;
public String name;
@OneToMany(mappedBy="regUsers")
public Set<RegId> regIds;
}
您可以查询具有至少个 RegId
的RegUsers
,如下所示:
you can query for RegUsers
that have at least one RegId
like this:
RegUsers.find.where().isNotNull("regIds.id").findList()
如果要查找与完全相同的 RegId
的RegUsers
,则确实需要编写自定义sql.
if you want to find RegUsers
with exactly one RegId
then you need to write custom sql indeed.
这篇关于外键查询播放!框架2. + ebean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!