0
我已經對使用HibernateHibernate的查詢問題
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;
}
}
也USERID中的.hbm.xml定義爲
<id name="userid" type="java.lang.Integer">
<column name="userid"/>
<generator class="identity"/>
</id>
web服務下面的代碼因此,我們有用戶和其他的表其FK爲userId的表。
我需要更新代碼來處理帶有用戶名的請求 - 然後檢索userId而不是web服務的上述代碼。
請幫助:)
約阿夫
你需要的查詢。你有沒有讀過hibernate文檔?你有什麼嘗試。您在關於查詢的文檔中不理解什麼?參見http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#queryhql –