比如:论坛的帖子类 Topic, 会关联一个用户类 User
public Class Topic //论坛帖子类
{
  private User postuser;
  .......................
}在数据访问层里 TopicDAO  两个方法: GetTopicById(int id)  GetTopicListTopic  类的 postuser 大家怎么从数据库取得的. 用UserDAO 调用 GetUserById(int id)还是直接从: 直接从SQL语句 连接 user 表(select * from topic join user on topic.postuser=user.id  where topic.id=id ) 设置 Topic 类的 Postuser 属性呢谢谢

解决方案 »

  1.   

    因为postuser是私有的
    最好调用响应方法设置它
    这样程序容易维护
      

  2.   

    如果调用 UserDAO 获取相应的 User对象的话 
    GetTopicList(forumId) 
    ResultSet r = stmt.executeQuery("SELECT * FROM Topic where forum=forumId");
    while(rs.next)
    {
      Topic topic=New Topic();
      topic.setId(rs.getInt("id"))
      topic.setTitle(rs.getString("title"));
      //如果返回10个topic话必然UserDAO的getUserById再执行10次SQL查询
      topic.setPostUser(UserDAO.getUserById(rs.getInt("userId")));
      .............................
    }
    这样效率会不会影响很大