使用hibernate获取两个都与逻辑外键(引用键)链接的对象

时间:2013-08-10 12:37:59

标签: hibernate

我是hibernate的新手,我的表格如下:

Tb_User

  • UserId(PK)
  • USERFIRSTNAME
  • USERLASTNAME
  • 用户登陆
  • UserPwd

Tb_Order

  • OrderId(PK)
  • UserId(逻辑FK)
  • 订购日期
  • OrderRemarks

我的Dao课程的功能:

public List<TbOrder> getUserOrdering(TbOrder tbOrder) throws Exception{
    Criteria _criteria = getSession().createCriteria(TbOrder.class);

    if(tbOrder.getUserId() != null)
        _criteria.add(Restrictions.eq("UserId", tbOrder.getUserId()));
    return (List<TbOrder>) _criteria.list();    
}

我的实体课程:

class TbOrder {
    Long orderId;
    Long userId;

    public TbOrder(){

    }
    public TbOrder(Long orderId, Long userId){
        this.orderId = orderId;
        this.userId = userId;
    }

    public Long getOrderId() {
        return orderId;
    }
    public void setOrderId(Long orderId) {
        this.orderId = orderId;
    }
    public Long getUserId() {
        return userId;
    }
    public void setUserId(Long userId) {
        this.userId = userId;
    }
}

class TbUser{
    private Long userId;
    private String userFirstName;
    private String userLastName;
    private String userLogin;
    private String userPwd;

    public TbUser() {

    }

    public TbUser(Long userId, String userFirstName, String userLastName,
            String userLogin, String userPwd) {
        this.userId = userId;
        this.userFirstName = userFirstName;
        this.userLastName = userLastName;
        this.userLogin = userLogin;
        this.userPwd = userPwd;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUserFirstName() {
        return userFirstName;
    }

    public void setUserFirstName(String userFirstName) {
        this.userFirstName = userFirstName;
    }

    public String getUserLastName() {
        return userLastName;
    }

    public void setUserLastName(String userLastName) {
        this.userLastName = userLastName;
    }

    public String getUserLogin() {
        return userLogin;
    }

    public void setUserLogin(String userLogin) {
        this.userLogin = userLogin;
    }

    public String getUserPwd() {
        return userPwd;
    }

    public void setUserPwd(String userPwd) {
        this.userPwd = userPwd;
    }
}

假设应将Tb_Order.UserId设置为链接到Tb_User的外键,但由于某些原因,我不能这样做。

但是,这是我的常规,我想获得用户的第一个名字和姓氏,他们已经下了id = 1001的订单,还有订单的日期和备注。通常情况下,当我谷歌搜索一段时间,有人告诉我,我可以尝试id.userid之类的东西来获取Tb_User对象,但是,我没有得到任何由hibernate生成的Id类,它应该是。那么,请告诉我什么是获得订单和用户信息的最佳方式?谢谢!

1 个答案:

答案 0 :(得分:0)

尝试修改你的Dao类的功能:

public List <Tb_User>   getUserOrdering(TbOrder tbOrder) throws Exception{
    Criteria _criteria = getSession().createCriteria(Tb_User.class);

    if(tbOrder.getUserId() != null)
        _criteria.add(Restrictions.eq("UserId", tbOrder.getUserId()));
    return (List<Tb_User>) _criteria.list();    
}

您将获得的List <Tb_User>,迭代它,并从Tb_User对象列表中,您可以使用getter方法获取用户的名字和姓氏。

注意:我假设您传递的 TbOrder tbOrder 对象中包含所需的UserId。