关于一个领域模型的问题,请各位帮忙.
最近在做一个项目,有如下一个需求:
用户可以加对方为好友。
我采用hibernate映射,模型如下:
public class Account{
private String id ;
private String userName;
private String password ;
......
@ManyToMany(fetch=FetchType.lazy)//这个地方级联关系应该如何设置呢?
private Collection<Account> friends;
setter/getter.....
}
public class UserCommonServiceImple implements UserCommonService{
public void addToMyFriend(Account myselfArg,Account friendArg){
//myself,以及friend都是从表现层传过来的,不完整,这样就需要查询数据。
myself = this.getAccountRepository().query(myselfArg.getId());
friend = this.getAccountRepository().query(friendArg.getId());
myself.getFriends().add(friend);
this.getAccountRepository().update(myself);
//问题就出在这里.每次加好友的时候首先要查询两次数据库,把从表现层传来的两个账号填充完整,然后还要把已经的好友全部加载出来,那么如果好友很多的话,每次加载好友很耗费性能。并且这样一个加好友的功能要查询多次数据库,太耗费性能了,请各位帮忙。多谢了。
}
}
请问各位这个问题怎么解决,有没有更好的方法。请各位能在百忙之中抽点时间帮个忙,多谢。
[该贴被javaEE于2008-09-08 15:35修改过]