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

» JWorld@TW » Object Relational Mapping » Hibernate  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
話題被移動
該話題已被移動 - koji , 2010-02-15 19:40
如果您尚不清楚該話題被移動的原因,請參考論壇規則以及本版公告或者聯系本版版主。
本主題所含的標籤
作者 [已解決]有關hibernate在spring dao下存取db的問題
cscxxx553





發文: 21
積分: 0
於 2010-02-15 11:47 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
問題描述:
目的是利用struts tag存取多對一關聯資料庫並將資料顯示於jsp面頁上
有三個class分別是customer、product及order
我在order裡有加入customer和product及set/get
並在order.hbm.xml設定
1
2
3
<many-to-one name="customer" column="customerid" cascade="save-update" class="com.dec.bean.Customer"></many-to-one>
 
<many-to-one name="product" column="productid" fetch="select" class="com.dec.bean.Product"></many-to-one>


jsp上面程式碼如下
1
2
3
4
5
6
<s:iterator value="#request.list" id="us">
      <tr>
        <td><s:property value="#us.product.productname"/></td>
                                <td><s:property value="#us.customer.customername"/></td>
                       </tr>
</s:iterator>


listOrderAction如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class ListOrderAction extends ActionSupport {
 
  private OrderService orderService;
 
  public OrderService getOrderService() {
    return orderService;
  }
  public void setOrderService(OrderService orderService) {
    this.orderService = orderService;
  }
  
  @SuppressWarnings("unchecked")
  @Override
  public String execute() throws Exception{
    
    Map request=(Map)ActionContext.getContext().get("request");
    request.put("list", orderService.findAll());
    return SUCCESS;
  }


執行結果:
order表格的資料有顯示但我想要的customername和productname卻不會顯示
看myclipse裡也只有發一條sql並沒搜尋另外二個表
請問該如何設定才能一併找出多對一相關聯的表資料

我的環境:
myeclipse8,tomcat6,jdk6,
spring2.0,hibernate3.3.2,struts2.0.14
postgresql8.4

src架構(包):
action
bean
service
serviceImp
dao
daoImp


cscxxx553 edited on 2010-02-16 19:59
reply to postreply to post
作者 Re:[請教]有關hibernate在spring dao下存取db的問題 [Re:cscxxx553]
koji

秒速5センチメートル

站長

發文: 8420
積分: 19
於 2010-02-15 19:40 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
他發的Sql長怎樣?
用HQL下fetch join可以嗎?有點忘了many to one預設會怎樣Tongue

koji


reply to postreply to post
JCConf Taiwan 2015 開始售票了!!
Facebook上的TWJUG社團,歡迎加入
作者 Re:[請教]有關hibernate在spring dao下存取db的問題 [Re:koji]
cscxxx553





發文: 21
積分: 0
於 2010-02-15 22:04 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
欄位不少,但只有從decorder抓出資料而已
要下hql嗎?我還以為可以很方便的使用hibernate設定方式達到功能
1
2
3
4
5
6
7
8
9
Hibernate: select order0_.orderid as orderid3_, order0_.orderquantity as orderqua2_3_,
order0_.orderdemanddate as orderdem3_3_, order0_.orderarrivaldate as orderarr4_3_, 
order0_.employeeid1 as employeeid5_3_, order0_.employeeid2 as employeeid6_3_, 
order0_.employeeid3 as employeeid7_3_, order0_.employeeid4 as employeeid8_3_, 
order0_.orderreviewdate as orderrev9_3_, order0_.orderreceivedate as orderre10_3_, 
order0_.ordermemo as ordermemo3_, order0_.orderclosedate as ordercl12_3_, 
order0_.customerid as customerid3_, order0_.freightid as freightid3_, 
order0_.productid as productid3_ 
from decorder order0_ order by order0_.orderdemanddate


reply to postreply to post
作者 Re:[請教]有關hibernate在spring dao下存取db的問題 [Re:cscxxx553]
javaZero





發文: 159
積分: 0
於 2010-02-15 23:24 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
Post is deleted

javaZero edited on 2010-06-24 13:13
reply to postreply to post
fortune is the act of good well.
作者 Re:[請教]有關hibernate在spring dao下存取db的問題 [Re:cscxxx553]
koji

秒速5センチメートル

站長

發文: 8420
積分: 19
於 2010-02-16 00:33 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
加上
Hibernate.initialize(order.product);
Hibernate.initialize(order.customer);
看他會不會load進來

因為也有可能沒設好Tongue

koji


reply to postreply to post
JCConf Taiwan 2015 開始售票了!!
Facebook上的TWJUG社團,歡迎加入
作者 Re:[請教]有關hibernate在spring dao下存取db的問題 [Re:javaZero]
cscxxx553





發文: 21
積分: 0
於 2010-02-16 11:30 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
這個應該是 hibernate 預設動作
我並沒有額外設定取別名所以我也覺得有點困惑


reply to postreply to post
作者 Re:[請教]有關hibernate在spring dao下存取db的問題 [Re:koji]
cscxxx553





發文: 21
積分: 0
於 2010-02-16 12:13 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
感謝二位高手的幫助
現在問題已解決了
解決方法如下:
1
2
<many-to-one name="customer" column="customerid" lazy="false"></many-to-one>
<many-to-one name="product" column="productid" lazy="false"></many-to-one>

本來我一直認為many-to-one的這一邊 lazy是自動設為false
因為如果通常要取出many方那勢必要有one方的關聯吧
真是想不透呀


reply to postreply to post
作者 Re:[請教]有關hibernate在spring dao下存取db的問題 [Re:cscxxx553]
mylee





發文: 764
積分: 1
於 2010-02-16 12:59 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
cscxxx553 wrote:
感謝二位高手的幫助
現在問題已解決了
解決方法如下:
1
2
<many-to-one name="customer" column="customerid" lazy="false"></many-to-one>
<many-to-one name="product" column="productid" lazy="false"></many-to-one>

本來我一直認為many-to-one的這一邊 lazy是自動設為false
因為如果通常要取出many方那勢必要有one方的關聯吧
真是想不透呀


小小建議
customer 應該用 bidirectional one-to-many,而不是 many-to-one。
customer has Order 比較自然, 從 Order 也要查 Customer 是用 bidirectional。
Order 和 Product 是 many-to-many,這通常拆成 Order (<- one-to-many -> ) OrderItem
以及 OrderItem (<- many-to-one -> ) Product


reply to postreply to post
作者 Re:[請教]有關hibernate在spring dao下存取db的問題 [Re:mylee]
cscxxx553





發文: 21
積分: 0
於 2010-02-16 13:08 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
謝謝您的建議
這應該是做成雙向比較常用的方式吧
本來還在思考雙向的狀況應該怎麼設比較好
經過您一說明我大概瞭解如何做成雙向


reply to postreply to post
» JWorld@TW »  Object Relational Mapping » Hibernate

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

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8