Web services Hibernate查询问题

Web services Hibernate查询问题,web-services,hibernate,Web Services,Hibernate,我在使用Hibernate的Web服务上有以下代码 Session session = ICDBHibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); User user = (User) session.load(User.class, userid); userId is a paramter sent from the client. User class package com

我在使用Hibernate的Web服务上有以下代码

Session session = ICDBHibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
User user = (User) session.load(User.class, userid);

userId is a paramter sent from the client.
User class
package com.icdb.data;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class User implements java.io.Serializable {

    private Integer userid;
    private String username;
    private String email;
    private String password;
    private String firstname;
    private String lastname;
//  private String streetaddress;
//  private String city;
//  private String phone;
    private String state;
    private String country;
    private Date birthdate;
//  private String zipcode;
    private Set usersesForFavoriteuser = new HashSet(0);
    private Set recipetipses = new HashSet(0);
    private Set recipeses = new HashSet(0);
    private Set recipepictureses = new HashSet(0);
    private Set onlineuserses = new HashSet(0);
    private Set generaltipses = new HashSet(0);
    private Set usersesForFollowinguser = new HashSet(0);
    private Set recipereviewses = new HashSet(0);

    public User() {
    }

    public User(    String username, 
                    String password, 
                    String email, 
                    String firstname,
                    String lastname, 
//                  String streetaddress, 
//                  String city, 
//                  String phone,
                    String state, 
                    String country, 
                    Date birthdate )
//                  String zipcode ) 
    {
        this.username = username;
        this.password = password;
        this.email = email;
        this.firstname = firstname;
        this.lastname = lastname;
//      this.streetaddress = streetaddress;
//      this.city = city;
//      this.phone = phone;
        this.state = state;
        this.country = country;
        this.birthdate = birthdate;
//      this.zipcode = zipcode;
    }

    public User(    String username,
                    String password, 
                    String email,
                    String firstname,
                    String lastname,
//                  String streetaddress, 
//                  String city, 
                    String phone,
//                  String state, 
//                  String country, 
//                  String email, 
                    Date birthdate,
//                  String zipcode, 
                    Set usersesForFavoriteuser, 
                    Set recipetipses,
                    Set recipeses, 
                    Set recipepictureses, 
                    Set onlineuserses,
                    Set generaltipses, 
                    Set usersesForFollowinguser, 
                    Set recipereviewses ) 
    {
        this.username = username;
        this.password = password;
        this.email = email;
        this.firstname = firstname;
        this.lastname = lastname;
//      this.streetaddress = streetaddress;
//      this.city = city;
//      this.phone = phone;
        this.state = state;
        this.country = country;
        this.birthdate = birthdate;
//      this.zipcode = zipcode;
        this.usersesForFavoriteuser = usersesForFavoriteuser;
        this.recipetipses = recipetipses;
        this.recipeses = recipeses;
        this.recipepictureses = recipepictureses;
        this.onlineuserses = onlineuserses;
        this.generaltipses = generaltipses;
        this.usersesForFollowinguser = usersesForFollowinguser;
        this.recipereviewses = recipereviewses;
    }

    public Integer getUserid() {
        return this.userid;
    }

    public void setUserid(Integer userid) {
        this.userid = userid;
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFirstname() {
        return this.firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return this.lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

//  public String getStreetaddress() {
//      return this.streetaddress;
//  }

//  public void setStreetaddress(String streetaddress) {
//      this.streetaddress = streetaddress;
//  }

//  public String getCity() {
//      return this.city;
//  }

//  public void setCity(String city) {
//      this.city = city;
//  }

//  public String getPhone() {
//      return this.phone;
//  }

//  public void setPhone(String phone) {
//      this.phone = phone;
//  }

    public String getState() {
        return this.state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCountry() {
        return this.country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getBirthdate() {
        return this.birthdate;
    }

    public void setBirthdate(Date birthdate) {
        this.birthdate = birthdate;
    }

//  public String getZipcode() {
//      return this.zipcode;
//  }

//  public void setZipcode(String zipcode) {
//      this.zipcode = zipcode;
//  }

    public Set getUsersesForFavoriteuser() {
        return this.usersesForFavoriteuser;
    }

    public void setUsersesForFavoriteuser(Set usersesForFavoriteuser) {
        this.usersesForFavoriteuser = usersesForFavoriteuser;
    }

    public Set getRecipetipses() {
        return this.recipetipses;
    }

    public void setRecipetipses(Set recipetipses) {
        this.recipetipses = recipetipses;
    }

    public Set getRecipeses() {
        return this.recipeses;
    }

    public void setRecipeses(Set recipeses) {
        this.recipeses = recipeses;
    }

    public Set getRecipepictureses() {
        return this.recipepictureses;
    }

    public void setRecipepictureses(Set recipepictureses) {
        this.recipepictureses = recipepictureses;
    }

    public Set getOnlineuserses() {
        return this.onlineuserses;
    }

    public void setOnlineuserses(Set onlineuserses) {
        this.onlineuserses = onlineuserses;
    }

    public Set getGeneraltipses() {
        return this.generaltipses;
    }

    public void setGeneraltipses(Set generaltipses) {
        this.generaltipses = generaltipses;
    }

    public Set getUsersesForFollowinguser() {
        return this.usersesForFollowinguser;
    }

    public void setUsersesForFollowinguser(Set usersesForFollowinguser) {
        this.usersesForFollowinguser = usersesForFollowinguser;
    }

    public Set getRecipereviewses() {
        return this.recipereviewses;
    }

    public void setRecipereviewses(Set recipereviewses) {
        this.recipereviewses = recipereviewses;
    }

}
在.hbm.xml中定义的userId也是

  <id name="userid" type="java.lang.Integer">
   <column name="userid"/>
   <generator class="identity"/>
  </id>

所以我们有一个用户表和另一个表,它的FK是userId

我需要更新代码以使用用户名处理请求,然后检索userId,而不是所提到的Web服务代码

请帮忙:)


Yoav

您需要一个查询。你读过hibernate文档了吗?你试过什么。文档中关于查询的哪些内容您不了解?看见
// I assume username is unique

// username is a parameter

// to get the full user object by name
String queryString = "from User where username = :username";
Query query = session.createQuery(queryString);
query.setString(":username", username);
User user = (User) query.uniqueResult();


// to get just the id
String queryString = "userid from User where username = :username";
Query query = session.createQuery(queryString);
query.setString(":username", username);
Integer userid = (Integer) query.uniqueResult();