昨天在回復中我寫了一個別人給我的解決方案。這種方法簡單,但是在數據庫的結構上不時很好。
我自己也找了一個方法,但是比較麻煩。
從數據庫底層做起,原來我的數據庫設計有問題。
我在用PowerDesigner建立概念模型的時候有一定問題。對于Friend表我用的是Entity,其實這個關系不應該是Entity,而應該是Association。如下圖:

這樣在生成物理數據模型時就會把 host,friend一起作為PK,如果用Entity就只能是FK
這樣的結構如果你用工具生成映射文件和Java文件時就會多出一個FriendID的類,里面有兩個元素就是host和friend
Friend.hbm.xml的結構如下
<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;
}
}
這樣你在添加好友列表時要這樣:
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);
因為f并不知道是誰加了它!
我自己也找了一個方法,但是比較麻煩。
從數據庫底層做起,原來我的數據庫設計有問題。
我在用PowerDesigner建立概念模型的時候有一定問題。對于Friend表我用的是Entity,其實這個關系不應該是Entity,而應該是Association。如下圖:

這樣在生成物理數據模型時就會把 host,friend一起作為PK,如果用Entity就只能是FK
這樣的結構如果你用工具生成映射文件和Java文件時就會多出一個FriendID的類,里面有兩個元素就是host和friend
Friend.hbm.xml的結構如下



























































































Friends.java如下:















































這樣你在添加好友列表時要這樣:

























注意不能 Host.getFriends().add(f);
因為f并不知道是誰加了它!