黑靈客棧
黑靈的沒啥技術(shù)含量的技術(shù)博客! -> http://zjumty.iteye.com
BlogJava
|
首頁
|
發(fā)新隨筆
|
發(fā)新文章
|
聯(lián)系
|
聚合
|
管理
隨筆:204 文章:2 評論:243 引用:0
對于昨天User -- Friend關(guān)系的解決!
昨天在回復(fù)中我寫了一個(gè)別人給我的解決方案。這種方法簡單,但是在數(shù)據(jù)庫的結(jié)構(gòu)上不時(shí)很好。
我自己也找了一個(gè)方法,但是比較麻煩。
從數(shù)據(jù)庫底層做起,原來我的數(shù)據(jù)庫設(shè)計(jì)有問題。
我在用PowerDesigner建立概念模型的時(shí)候有一定問題。對于Friend表我用的是Entity,其實(shí)這個(gè)關(guān)系不應(yīng)該是Entity,而應(yīng)該是Association。如下圖:
這樣在生成物理數(shù)據(jù)模型時(shí)就會(huì)把 host,friend一起作為PK,如果用Entity就只能是FK
這樣的結(jié)構(gòu)如果你用工具生成映射文件和Java文件時(shí)就會(huì)多出一個(gè)FriendID的類,里面有兩個(gè)元素就是host和friend
Friend.hbm.xml的結(jié)構(gòu)如下
<
hibernate-mapping
>
<!--
Auto-generated mapping file from
the hibernate.org cfg2hbm engine
-->
<
class
name
="org.netcatcher.web.model.Friends"
table
="Friends"
schema
="nc"
catalog
="NetCatcher"
>
<
composite-id
name
="id"
class
="org.netcatcher.web.model.FriendsId"
>
<
key-many-to-one
name
="Host"
class
="org.netcatcher.web.model.Customers"
>
<
column
name
="host"
length
="100"
not-null
="false"
/>
</
key-many-to-one
>
<
key-many-to-one
name
="Friend"
class
="org.netcatcher.web.model.Customers"
>
<
column
name
="friend"
length
="100"
not-null
="false"
/>
</
key-many-to-one
>
</
composite-id
>
</
class
>
</
hibernate-mapping
>
FriendID.java如下
public
class
FriendsId implements java.io.Serializable
{
/**/
/*
*
*
*/
private
static
final
long
serialVersionUID
=
3689069555917795889L
;
//
Fields
private
org.netcatcher.web.model.Customers Host;
private
org.netcatcher.web.model.Customers Friend;
//
Constructors
/**/
/*
* default constructor
*/
public
FriendsId()
{
}
//
Property accessors
/**/
/*
*
*/
public
org.netcatcher.web.model.Customers getHost ()
{
return
this
.Host;
}
public
void
setHost(org.netcatcher.web.model.Customers Host)
{
this
.Host
=
Host;
}
/**/
/*
*
*/
public
org.netcatcher.web.model.Customers getFriend ()
{
return
this
.Friend;
}
public
void
setFriend (org.netcatcher.web.model.Customers Friend)
{
this
.Friend
=
Friend;
}
public
boolean equals(Object other)
{
if
( (
this
==
other ) )
return
true
;
if
( (other
==
null
) )
return
false
;
if
(
!
(other instanceof FriendsId) )
return
false
;
FriendsId castOther
=
( FriendsId ) other;
return
(
this
.getHost()
==
castOther.getHost())
||
(
this
.getHost()
==
null
?
false
: (castOther.getHost()
==
null
?
false
:
this
.getHost().equals(castOther.getHost())))
&&
(
this
.getFriend()
==
castOther.getFriend())
||
(
this
.getFriend()
==
null
?
false
: (castOther.getFriend()
==
null
?
false
:
this
.getFriend().equals(castOther.getFriend())));
}
public
int
hashCode()
{
int
result
=
17
;
result
=
37
*
result
+
this
.getHost().hashCode();
result
=
37
*
result
+
this
.getFriend().hashCode();
return
result;
}
}
Friends.java如下:
public
class
Friends implements java.io.Serializable
{
//
Fields
/**/
/*
*
*
*/
private
static
final
long
serialVersionUID
=
3258409517246395959L
;
private
org.netcatcher.web.model.FriendsId id;
//
Constructors
/**/
/*
* default constructor
*/
public
Friends()
{
}
/**/
/*
* constructor with id
*/
public
Friends(org.netcatcher.web.model.FriendsId id)
{
this
.id
=
id;
}
//
Property accessors
/**/
/*
*
*/
public
org.netcatcher.web.model.FriendsId getId ()
{
return
this
.id;
}
public
void
setId (org.netcatcher.web.model.FriendsId id)
{
this
.id
=
id;
}
}
這樣你在添加好友列表時(shí)要這樣:
public
void
addFriend(String hostEmail,String friendEmail)
throws InfrastructureException
{
try
{
HibernateUtil.beginTransaction();
Customers host
=
(Customers)HibernateUtil.getSession()
.load(Customers.
class
,hostEmail);
Customers friend
=
(Customers)HibernateUtil.getSession()
.load(Customers.
class
,friendEmail);
FriendsId fid
=
new
FriendsId();
fid.setHost(host);
fid.setFriend(friend);
Friends f
=
new
Friends(fid);
HibernateUtil.getSession().saveOrUpdate(f);
HibernateUtil.commitTransaction();
}
catch
(HibernateException e)
{
throw
new
InfrastructureException(e);
}
}
注意不能 Host.getFriends().add(f);
因?yàn)閒并不知道是誰加了它!
發(fā)表于 2005-04-28 14:00
黑靈
閱讀(559)
評論(0)
編輯
收藏
所屬分類:
ORM
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關(guān)文章:
Hibernate3中取得多層數(shù)據(jù)的所產(chǎn)生的n+1 selects問題的解決。
Hibernate3中的更新與刪除
Could not initialize proxy - the owning Session was closed
iBatis實(shí)戰(zhàn)小結(jié)!
一個(gè)困擾了我好幾天的Hibernate+Spring的問題,算是解決了吧!
對于昨天User -- Friend關(guān)系的解決!
這樣的表結(jié)構(gòu)Hibernate映射應(yīng)該是什么樣子?
Hibernate用Mysql的中文問題
caveatemptor中的HibernateUtil
<
2005年4月
>
日
一
二
三
四
五
六
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
公告
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
隨筆分類
(175)
AJAX(6)
(rss)
DataBase(5)
(rss)
Do everything with Groovy(4)
(rss)
ExtJS
(rss)
J2EE(29)
(rss)
JSF(4)
(rss)
MStar Utility(2)
(rss)
ORM(9)
(rss)
RIA(3)
(rss)
Server配置(2)
(rss)
Struts(1)
(rss)
Tapestry(1)
(rss)
Unix&Linux(32)
(rss)
wap(1)
(rss)
WebWork(4)
(rss)
不編不知道,一編嚇一跳(17)
(rss)
亂七八糟(32)
(rss)
客戶端技術(shù)(1)
(rss)
正則表達(dá)式
(rss)
腳本語言(22)
(rss)
文章分類
(1)
Tomcat(1)
(rss)
博客同道
GENOW
(rss)
遠(yuǎn)離塵囂
常用資源
Martin Fowler
Martin Fowler的文章
最新評論
1.?re: 關(guān)于spring-mvc的InitBinder注解的參數(shù)
這個(gè)里面User與User_是兩個(gè)不同的類
--mmocake
Powered by:
博客園
模板提供:
滬江博客
Copyright ©2025 黑靈
主站蜘蛛池模板:
日韩视频在线免费
|
亚洲国产日韩在线一区
|
成人毛片免费观看
|
久久久国产精品福利免费
|
日本亚洲高清乱码中文在线观看
|
老汉色老汉首页a亚洲
|
国产亚洲精品拍拍拍拍拍
|
国产成人涩涩涩视频在线观看免费
|
亚洲视频一区调教
|
亚洲精品97久久中文字幕无码
|
成年男女男精品免费视频网站
|
性色午夜视频免费男人的天堂
|
一级毛片大全免费播放下载
|
亚洲欧美成aⅴ人在线观看
|
亚洲高清美女一区二区三区
|
久久亚洲国产午夜精品理论片
|
国产国拍亚洲精品福利
|
99re热免费精品视频观看
|
久久免费的精品国产V∧
|
国产永久免费高清在线
|
亚美影视免费在线观看
|
国产午夜亚洲精品不卡
|
亚洲av无码专区亚洲av不卡
|
亚洲永久网址在线观看
|
亚洲看片无码在线视频
|
亚洲Av无码一区二区二三区
|
久久久久亚洲AV无码观看
|
亚洲国产精品线在线观看
|
久久久久久a亚洲欧洲AV
|
亚洲AV一宅男色影视
|
亚洲日韩精品射精日
|
亚洲人成网站在线播放vr
|
国产亚洲大尺度无码无码专线
|
国产偷窥女洗浴在线观看亚洲
|
亚洲熟女乱综合一区二区
|
久久精品亚洲福利
|
亚洲色大成网站WWW久久九九
|
国产日产亚洲系列
|
亚洲色欲色欲www在线丝
|
久久精品国产96精品亚洲
|
久久亚洲精品人成综合网
|