Hibernate:父实体上的过滤器是否隐式过滤子实体?

发布于 2024-10-05 00:49:57 字数 774 浏览 0 评论 0原文

假设有两个实体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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪少女 2024-10-12 00:49:57

如果 EntityB 从未直接查询,但始终通过 entityA.getEntityBList() 加载,则您的过滤器将起作用。但如果直接查询EntityB,结果将不会被过滤。将过滤器视为限制子句,包含在 Hibernate 触发的所有 SQL 的 WHERE 部分中以加载 EntityA

If EntityB is never queried directly, but is always loaded via entityA.getEntityBList() your filter will work. But if you directly query for EntityB, the results won't be filtered. Think of filters as restriction clauses included in the WHERE part of all SQL fired by Hibernate to load EntityA.

故事和酒 2024-10-12 00:49:57

我也有同样的问题。事实证明,您需要为 EntityB 定义一个过滤器,在其中显式引用 EntityA,例如

<filter 
    name="myFilter" 
    condition="((select a.myProperty from EntityA a where a.id = entityA_id) = :myPropertyParam)" 
/>

,如您所见,我使用 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

<filter 
    name="myFilter" 
    condition="((select a.myProperty from EntityA a where a.id = entityA_id) = :myPropertyParam)" 
/>

As you can see I've used xml for my hibernate definitions. I'm sure that it's also possible with annotations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文