繁体   English   中英

HQL等效查询与标准查询在休眠中有多对多关系吗?

[英]HQL equivalent query to criteria query in hibernate many to many relationship?

我有两个表库存和类别,它们之间具有多对多关系。

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>

Category.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"

让我知道您是否需要更多详细信息。

好的,显然,您错过了文档中有关连接的部分:

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

我打算彻底调查此问题。 我现在从以前的答案中怀疑的是,join语句中应该有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 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM