我有一个从教程中获得的登录代码,在该教程中,用户表具有所有需要的数据。就我而言,我创建了一个具有用户名,profileid和密码的用户表,以及另一个具有具有与该用户相关的所有数据的配置文件的表。我需要做的是在登录完成后获取用户profileid,这意味着当结果返回登录的userList时,我想获取idprofile以使用它并获取配置文件数据:
UsermanagerDAOImpl:
public User getUser(String username) {
List<User> userList = new ArrayList<User>();
Query query = openSession().createQuery("from User u where u.username = :username");
query.setParameter("username", username);
userList = query.list();
if (userList.size() > 0)
return userList.get(0);
// Don't know what to do here
else
return null;
}
public Profile getProfilebyId(int idprofile) {
List list = getSessionFactory().getCurrentSession().createQuery("from profile where idprofile=?").setParameter(
THIS IS SUPPOSED TO BE THE IDPROFILE AS RESULT FROM LOGIN SUCCESS OF GETUSER METHOD, idprofile).list();
if ( list.size() == 0 )
return null;
else
return (Profile)list.get(0);
}
模型 :
public class Usermanager {
@Entity
@Table(name="USER")
public class User implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
int iduser;
String username;
String password;
int idprofile;
int accountstatus;
public int getIduser() {
return iduser;
}
public void setIduser(int iduser) {
this.iduser = iduser;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getIdprofile() {
return idprofile;
}
public void setIdprofile(int idprofile) {
this.idprofile = idprofile;
}
public int getAccountstatus() {
return accountstatus;
}
public void setAccountstatus(int accountstatus) {
this.accountstatus = accountstatus;
}
}
@Entity
@Table(name="PROFILE")
public class Profile implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
int idprofile;
String nomprofile;
String prenprofile;
String mailprofile;
String adressprofile;
int phoneprofile;
Date datenaissanceprofile;
char sexeuser;
String imagepath;
public int getIdprofile() {
return idprofile;
}
public void setIdprofile(int idprofile) {
this.idprofile = idprofile;
}
public String getNomprofile() {
return nomprofile;
}
public void setNomprofile(String nomprofile) {
this.nomprofile = nomprofile;
}
public String getPrenprofile() {
return prenprofile;
}
public void setPrenprofile(String prenprofile) {
this.prenprofile = prenprofile;
}
public String getMailprofile() {
return mailprofile;
}
public void setMailprofile(String mailprofile) {
this.mailprofile = mailprofile;
}
public String getAdressprofile() {
return adressprofile;
}
public void setAdressprofile(String adressprofile) {
this.adressprofile = adressprofile;
}
public int getPhoneprofile() {
return phoneprofile;
}
public void setPhoneprofile(int phoneprofile) {
this.phoneprofile = phoneprofile;
}
public Date getDatenaissanceprofile() {
return datenaissanceprofile;
}
public void setDatenaissanceprofile(Date datenaissanceprofile) {
this.datenaissanceprofile = datenaissanceprofile;
}
public char getSexeuser() {
return sexeuser;
}
public void setSexeuser(char sexeuser) {
this.sexeuser = sexeuser;
}
public String getImagepath() {
return imagepath;
}
public void setImagepath(String imagepath) {
this.imagepath = imagepath;
}
}
您可以使用嵌套where子句查询配置文件表
from profile where profile.idprofile=(select u.idprofile from User u where u.username = :username)
或者,
您可以在User
和profile
表之间执行联接,并使用on子句查询它User.username
select p from User u join u.idprofile profile p where u.username = :username;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句