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
本主題所含的標籤
無標籤
作者 Hibernate及hibernate jpa ManyToOne問題
mmncsmm





發文: 12
積分: 0
於 2011-06-10 18:46 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
我將ManyToOne的資料,

用基本的Hibernate(xml設定)及Hibernate jpa實作取ManyToOne,印出sql後,

發現居然sql不一樣,都是Hibernate3.6

執行的code都一樣,如下
1
2
Announce a = ms.findAnnounceById(1);
LOG.debug(a.getMember().getName());


Hibernate 設定檔
1
2
3
<many-to-one name="member" class="div.mmncsmm.persistence.Member" fetch="select">
<column name="UserId" />
</many-to-one>


Hibernate jpa
1
2
3
4
5
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "UserId")
public Member getMember() {
  return this.member;
}


執行a.getMember().getName()後的sql如下
Hibernate 設定檔
1
2
3
4
5
6
7
8
9
10
select
  member0_.UserId as UserId0_0_,
  member0_.account as account0_0_,
  member0_.password as password0_0_,
  member0_.name as name0_0_,
  member0_.IsAdmin as IsAdmin0_0_ 
from
  dbo.Member member0_ 
where
  member0_.UserId=?


Hibernate jpa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
select 
  member0_.UserId as UserId0_1_, 
  member0_.account as account0_1_, 
  member0_.IsAdmin as IsAdmin0_1_, 
  member0_.name as name0_1_, 
  member0_.password as password0_1_, 
  announces1_.UserId as UserId0_3_, 
  announces1_.Aid as Aid3_, 
  announces1_.Aid as Aid1_0_, 
  announces1_.Content as Content1_0_, 
  announces1_.UserId as UserId1_0_, 
  announces1_.Title as Title1_0_ 
from 
  dbo.Member member0_ left outer join dbo.Announce announces1_ on member0_.UserId=announces1_.UserId 
where 
  member0_.UserId=?


很明顯用jpa會多 left outer join ,效能也比較差,
要去哪改設定呢,讓產生出來的sql跟用hibernate一樣,
兩個方法我都有使用OpenSessionInView,
所以我不想用FetchType.EAGER

有大大可以指點一下嘛
感謝


mmncsmm edited on 2011-06-10 18:49
reply to postreply to post
my blog : http://mmncsmm.blogspot.com
» 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