hql 查询触发的模型?

发布于 2024-10-09 05:48:34 字数 651 浏览 0 评论 0原文

我想了解 hibernate 如何在内部执行 hql 查询或在其他模型中 hql 查询引擎如何工作。请推荐一些好的链接?

阅读的原因之一是以下问题。

    Class Branch
    {

          //lazy loaded
          @joincolumn(name="company_id")
          Company company;
    } 

由于公司是重对象,因此它是延迟加载的。

现在我有 hql 查询,

       "from Branch as branch where branch.Company.id=:companyId"

我担心的是,如果要触发上述查询,hql 引擎必须检索公司对象,那么它会影响性能,我更愿意在 Branch 类中添加一个属性,即 companyId。因此,在这种情况下,hql 查询将是:

    "from Branch as branch where branch.companyId=:companyId"

如果 hql 引擎首先从 hql 生成 sql,然后触发 sql 查询本身,那么应该不会有性能问题。

如果问题无法理解,请告诉我。

I want to understand how hibernate execute hql query internally or in other models how hql query engine works. Please suggest some good links for same?

One of reason for reading is following problem.

    Class Branch
    {

          //lazy loaded
          @joincolumn(name="company_id")
          Company company;
    } 

Since company is heavy object so it is lazy loaded.

now i have hql query

       "from Branch as branch where branch.Company.id=:companyId"

my concern is that if for firing above query, hql engine has to retrieve company object then its a performance hit and i would prefer to add one more property in Branch class i.e. companyId. So in this case hql query would be

    "from Branch as branch where branch.companyId=:companyId"

If hql engine first generate sql from hql followed by firing of sql query itself, then there should be no performance issue.

Please let me know if problem is not understandable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

青衫儰鉨ミ守葔 2024-10-16 05:48:34

我担心的是,如果要触发上述查询,hql 引擎必须检索公司对象那么它的性能会受到影响

因为您只想使用公司标识符(在纯 SQL 中,单个外键),Hibernate引擎足够智能可以在不加载公司实体的情况下使用公司外键

但是...

它只在使用属性访问策略时起作用。另一方面,您必须通过将每个 JPA 相关注释放在 getter 方法的正上方(而不是字段 - 如您的示例所示)来设置实体。发生这种情况是因为 Hibernate 在幕后通过子类化实体来使用代理。例如,如果您使用名为 id 的字段,该字段由名为 userName 的属性封装(是的,这是可能的),例如

 @Id
 private String id;

 public String getUserName() { return this.id; }
 public void setUserName(String userName) { this.id = userName; }

因为 Hibernate 用于管理实体的逻辑放置在其子类属性中, Hibernate 如何假设 userName 属性封装了一个名为 id 的字段???

因此,如果您想避免加载 Company,请使用属性访问策略。

My concern is that if for firing above query, hql engine has to retrieve company object then its a performance hit

Since you want just to use the Company identifier (in plain SQL, a single foreign key), Hibernate engine is smart enough to use Company foreign key without loading the Company entity

But...

It just work when using property access strategy. On the other hand, you have to set up your entity by putting each JPA related annotation right above the getter method (instead of the field - as shown by your example). It occurs because Hibernate, behind the scenes, makes use of proxies by subclassing your entities. If you use, for instance, a field named id encapsulated by a property called userName (yes, it is possible) such as

 @Id
 private String id;

 public String getUserName() { return this.id; }
 public void setUserName(String userName) { this.id = userName; }

Because the logic used by Hibernate to manage your entities is placed inside its subclass properties, How can Hibernate suppose that userName property encapsulates a field named id ???

So if you want to avoid loading Company, use property access strategy instead.

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