Hibernate:父实体上的过滤器是否隐式过滤子实体?
假设有两个实体EntityA和EntityB。 为两个实体定义了两个表。 EntityB 是 EntityA 的子实体。 现在在使用 hibernate 注释的 java pojos 中,我定义了与表 EntityA 和 EntityB 相对应的两个类,如下所示。
@FilterDef(name = "myfilter", parameters = { @ParamDef(name = "year", type = "int") }, defaultCondition = ":year = year")
public class EntityA
{
@OneToMany(mappedBy="EntityA")
List<EntityB> getEntityBList()
{
}
}
public class EntityB
{
@ManyToOne
@joincolumn(name="entityA_id")
EntityA getEntityA()
{
}
}
因为我已经对 EntityA 进行了过滤。 EntityB 是否隐含地仅加载与 EntityA 的过滤对象相对应的对象,同时牢记父子关系,或者是否需要在 EntityB 上显式放置过滤器?
示例:如果对 EntityA 进行过滤,则仅加载与 2010 年相对应的对象。我希望 EntityB 的加载对象应该是仅与 2010 年相对应的 EntityA 对象的子对象。
Suppose there are two entities EntityA and EntityB.
Two tables are defined for both entities.
EntityB is child entity of EntityA.
Now in java pojos using hibernate annotation, i have defined two classes corresponding to tables EntityA and EntityB as below.
@FilterDef(name = "myfilter", parameters = { @ParamDef(name = "year", type = "int") }, defaultCondition = ":year = year")
public class EntityA
{
@OneToMany(mappedBy="EntityA")
List<EntityB> getEntityBList()
{
}
}
public class EntityB
{
@ManyToOne
@joincolumn(name="entityA_id")
EntityA getEntityA()
{
}
}
Since I have got filter on EntityA. Is it implicit that EntityB will have objects loaded only which correspond to filtered objects of EntityA keeping in mind parent child relationship or do need to explicitly put filter on EntityB ?
Example if EntityA is filtered such that objects corresponding to year 2010 is loaded only. I want that loaded object of EntityB should be only which are child of EntityA objects corresponding to year 2010 only.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
EntityB
从未直接查询,但始终通过entityA.getEntityBList()
加载,则您的过滤器将起作用。但如果直接查询EntityB
,结果将不会被过滤。将过滤器视为限制子句,包含在 Hibernate 触发的所有 SQL 的WHERE
部分中以加载EntityA
。If
EntityB
is never queried directly, but is always loaded viaentityA.getEntityBList()
your filter will work. But if you directly query forEntityB
, the results won't be filtered. Think of filters as restriction clauses included in theWHERE
part of all SQL fired by Hibernate to loadEntityA
.我也有同样的问题。事实证明,您需要为 EntityB 定义一个过滤器,在其中显式引用 EntityA,例如
,如您所见,我使用 xml 作为我的 hibernate 定义。我确信使用注释也是可能的。
I had the same problem. It turned out that you need to define a filter for EntityB in which you explicitly make a reference to EntityA, for example
As you can see I've used xml for my hibernate definitions. I'm sure that it's also possible with annotations.