HQL等价查询到条件查询在hibernate中的多对多关系?

时间:2014-05-15 17:12:37

标签: java mysql hibernate

我有两个表库存和类别,彼此有多对多的关系。

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"

如果您需要更多详细信息,请与我们联系。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我打算彻底调查这个问题。我现在对前一个答案的疑问是,在join语句中应该有 ON Clause 。如果我的期望是错误的,请原谅我。

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