2015-09-05 65 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子句進行查詢
相關問題