JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Object Relational Mapping » Hibernate  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
作者 Re:Hibernate HQL Left Join [Re:qrtt1]
wildpeanut

tinyurl.com/lsyoy



發文: 162
積分: 0
於 2010-08-29 19:26 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
謝謝qrtt1詳細的回覆
我後來找了Hibernate in Action來看,我要做的功能在裡面叫Projection
裡面的例子是Item和Bid的例子,我把兩者的關聯設成一對多再利用以下的程式碼就產生類似報表的功能了,之前可能某的部分沒有設定正確 0rz

1
2
3
4
5
6
7
8
Session session = HibernateUtil.getSessionFactory().openSession();
Iterator i = session.createQuery(
        "select item.id, item.itemName, bid.bidAmount " +
        "from Item item left join item.bids bid " +
        "where bid.bidAmount > 100"
        ).list().iterator();
session.close();
HibernateUtil.getSessionFactory().close();


我漸漸覺得Hibernate是個好物了Big Smile
附上我設定的xml給其他人參考
ITEM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<hibernate-mapping>
    <class name="hibernate.orm.Item" table="ITEM">
        <id name="itemId" type="string">
            <column name="ITEM_ID" />
        </id>
        <property name="itemName" type="string">
            <column name="ITEM_NAME" />
        </property>
        <property name="createTime" type="timestamp">
            <column name="CREATE_TIME" />
        </property>
        <set name="bids" table="BID" cascade="save-update">
          <key column="ITEM_ID"/>
          <one-to-many class="hibernate.orm.Bid"/>
        </set>
    </class>
</hibernate-mapping>

BID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<hibernate-mapping>
    <class name="hibernate.orm.Bid" table="BID">
        <id name="bidId" type="string">
            <column name="BID_ID" />
        </id>
        <property name="itemId" type="string">
            <column name="ITEM_ID" />
        </property>
        <property name="bidAmount" type="long">
            <column name="BID_AMOUNT" />
        </property>
        <property name="userId" type="string">
            <column name="USER_ID" />
        </property>
    </class>
</hibernate-mapping>


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
10643 Hibernate HQL Left Join wildpeanut 579 2010-08-09 11:58
10311 Re:Hibernate HQL Left Join qrtt1 223 2010-08-09 13:08
10322 Re:Hibernate HQL Left Join wildpeanut 167 2010-08-09 13:18
10289 Re:Hibernate HQL Left Join qrtt1 535 2010-08-09 13:59
10266 Re:Hibernate HQL Left Join wildpeanut 431 2010-08-09 23:29
10472 Re:Hibernate HQL Left Join qrtt1 1951 2010-08-09 23:56
10233 Re:Hibernate HQL Left Join wildpeanut 1711 2010-08-29 19:26
» JWorld@TW »  Object Relational Mapping » Hibernate

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8