【问题标题】:What means "p" in select p... statement? [closed]select p... 语句中的“p”是什么意思? [关闭]
【发布时间】:2015-03-26 09:51:01
【问题描述】:
private void addPersonToEvent(Long personId, Long eventId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();

    Person aPerson = (Person) session
            .createQuery("select p from Person p left join fetch p.events where p.id = :pid")
            .setParameter("pid", personId)
            .uniqueResult(); // Eager fetch the collection so we can use it detached
    Event anEvent = (Event) session.load(Event.class, eventId);

    session.getTransaction().commit();

这段代码来自休眠参考。我不明白这个“select p”SQL 语句中的 p 是什么意思。 “p”是什么?

【问题讨论】:

    标签: java hibernate hql


    【解决方案1】:

    它只是一个别名,用于查询特定实体。这些别名用于 JPQL/HQL。这将被视为实体,您可以使用(点)来引用实体中的字段。

    【讨论】:

      【解决方案2】:

      这是 Hibernate 查询语言,或 HQL,它不应该像你正在做的那样被理解为普通的 SQL。

      P 是要返回的实体的别名。

      HQL查询(非SQL)基本解释:

      select p -- the entity to retrieve
      from Person p -- Person entity aliased p
      left join fetch p.events -- left join with entity Events, check the entity mapping between Person and Event entities
      where p.id = :pid -- p.id is the field id from Person entity, :pid is a named parameter called pid
      

      更多信息,请参考Hibernate HQL documentation

      【讨论】:

      • 谢谢。你能简单解释一下所有这些语句(select p from Person p left join fetch p.events where p.id = :pid)吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 2014-09-21
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      • 2011-08-31
      相关资源
      最近更新 更多