2015-09-05 71 views
1

我有一个从教程中获得的登录代码,在该教程中,用户表具有所有需要的数据。对于我的情况,我创建了一个拥有用户名和profileid和密码的用户表,以及另一个具有与该用户有关的所有数据的表。我需要做的是在登录完成时获取用户配置文件,表示结果返回登录用户列表时,我想要获取该配置文件以使用它并获取配置文件数据:用Java登录后获取用户配置文件

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; 
     } 

    } 

回答

1

您可以查询配置表与嵌套的where子句

from profile where profile.idprofile=(select u.idprofile from User u where u.username = :username) 


或者

你可以执行连接Userprofile之间的桌子和使用上User.username

select p from User u join u.idprofile profile p where u.username = :username; 
where子句进行查询