登录Java

时间:2015-09-05 22:22:01

标签: java

我有一个从教程中获得的登录代码,在该教程中,用户表中包含所有需要的数据。对于我的情况,我创建了一个具有用户名和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;
        }

    }

1 个答案:

答案 0 :(得分:1)

您可以使用嵌套的where子句

查询配置文件表
from profile where profile.idprofile=(select u.idprofile from User u where u.username = :username)




您可以在Userprofile表之间执行联接,并使用User.username上的where子句查询它

select p from User u join u.idprofile profile p where u.username = :username;