【问题标题】:HQL equivalent query to criteria query in hibernate many to many relationship?HQL等效查询到休眠多对多关系中的条件查询?
【发布时间】:2014-05-15 17:12:37
【问题描述】:

我有两张表 stock 和 category 彼此之间存在多对多关系。

Stock.hbm.xml

<hibernate-mapping>
<class name="hibernate.mapping.manytomany.Stock" table="stock">
    <id name="stockId" type="java.lang.Integer">
        <column name="STOCK_ID" />
        <generator class="identity" />
    </id>
    <property name="stockCode" type="string">
        <column name="STOCK_CODE" length="10" not-null="true" unique="true" />
    </property>
    <property name="stockName" type="string">
        <column name="STOCK_NAME" length="20" not-null="true" unique="true" />
    </property>
    <set name="categories" table="stock_category" 
        inverse="false" lazy="true" fetch="select" cascade="all" >
        <key>
            <column name="STOCK_ID" not-null="true" />
        </key>
        <many-to-many entity-name="hibernate.mapping.manytomany.Category">
            <column name="CATEGORY_ID" not-null="true" />
        </many-to-many>
    </set>
</class>
</hibernate-mapping>

类别.hbm.xml

<hibernate-mapping>
<class name="hibernate.mapping.manytomany.Category" table="category">
    <id name="categoryId" type="java.lang.Integer">
        <column name="CATEGORY_ID" />
        <generator class="identity" />
    </id>
    <property name="name" type="string">
        <column name="NAME" length="10" not-null="true" />
    </property>
    <property name="desc" type="string">
        <column name="[DESC]" not-null="true" />
    </property>
    <set name="stocks" table="stock_category" inverse="true" lazy="true"
                                 fetch="select">
        <key>
            <column name="CATEGORY_ID" not-null="true" />
        </key>
        <many-to-many entity-name="hibernate.mapping.manytomany.Stock">
            <column name="STOCK_ID" not-null="true" />
        </many-to-many>
    </set>
</class>
</hibernate-mapping>

这是我的条件查询,

Criteria c = session.createCriteria(Category.class, "c");
c.createAlias("c.stocks", "s");
c.add(Restrictions.eq("c.categoryId", 1));
c.setProjection(Projections.projectionList()
 .add(Projections.property("s.stockId"))
 .add(Projections.property("s.stockName")));

对于这种情况,我需要等效的 HQL..我已经尝试过了,但它给出了不同的结果,

String query = "select c.stocks.stockId, c.stocks.stockName from Category c where
                    c.categoryId=1"

如果您需要更多详细信息,请告诉我。

【问题讨论】:

  • 你试过什么?你读过the documentation吗?
  • 我只知道基于条件的查询。
  • 编写文档是为了让您可以从中学习。阅读。您认为开发人员如何学习新事物?魔法?不,通过阅读文档和尝试。
  • 好的,我会仔细阅读该文档。

标签: java mysql hibernate


【解决方案1】:

好的,很明显,您错过了文档中关于联接的部分:

select s.stockId,               // equivalent to the s.stockId projection
       s.stockName              // equivalent to the s.stockName projection
       from Category c          // equivalent to the root criteria creation
       join c.stocks s          // equivalent to the alias creation
       where c.categoryId = 1   // equivalent to the restriction addition

【讨论】:

    【解决方案2】:

    我计划彻底调查此问题。我现在从以前的答案中怀疑的是,加入语句中应该有 ON 子句。如果我的期望是错误的,请原谅我。

    select s.stockId,s.stockName from Category c join c.stocks s 
      on c.stockId=s.stockId where c.categoryId = 1 ;
      //it might be on c.CATEGORY_ID=s.CATEGORY_ID also 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      • 2019-12-17
      • 2013-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多