请帮我HQL加入查询

时间:2012-04-13 11:29:27

标签: hibernate join hql

我刚开始休眠。

我有以下课程:

@Entity
@Table( name = "Users" )
public class User implements Serializable{

private static final long serialVersionUID = -3550174731709532722L;

private String userName;
private String pasportID;
private Company company; 
private String name;
private String email;
private String phone1;
private String phone2;
private String password; //may be null/empty , will be kept hashed
private boolean isAdmin;
private Date lastLogin;


User() {} //not public on purpose!


public User(String userName, String pasportID, Company company,
        String name, String email, String phone1, String phone2,
        String password, boolean isAdmin) {
    this.userName = userName;
    this.pasportID = pasportID;
    this.company = company;
    this.name = name;
    this.email = email;
    this.phone1 = phone1;
    this.phone2 = phone2;
    this.password = password;
    this.isAdmin = isAdmin;
}


@Id
public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

@OneToOne(cascade = CascadeType.ALL)
public Company getCompany() {
    return company;
}

public void setCompany(Company company) {
    this.company = company;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}

public String getPhone1() {
    return phone1;
}
public void setPhone1(String phone1) {
    this.phone1 = phone1;
}

public String getPhone2() {
    return phone2;
}
public void setPhone2(String phone2) {
    this.phone2 = phone2;
}

public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}

public boolean isAdmin() {
    return isAdmin;
}

public void setAdmin(boolean isAdmin) {
    this.isAdmin = isAdmin;
}

@Temporal(TemporalType.TIMESTAMP) 
public Date getLastLogin() {
    return lastLogin;
}

public void setLastLogin(Date lastLogin) {
    this.lastLogin = lastLogin;
}


public String getPasportID() {
    return pasportID;
}


public void setPasportID(String pasportID) {
    this.pasportID = pasportID;
    }
}

@Entity
@Table( name = "COMPANIES" )
/**
 * This class is an entity class that represent the company object in the database.
 * This class holds no logic, only getters, setter and constructors.
 */
public class Company implements Serializable {


    private static final long serialVersionUID = -4809404779999217296L;

    private long companyID;
    private CompanyType companyType;
    private String name;

    Company() {} //not public on purpose!

    public Company(String name, CompanyType companyType) {
        this.companyType = companyType;
        this.name = name;
    }


    @Id
    @GeneratedValue(generator="increment")
    @GenericGenerator(name="increment", strategy = "increment")
    public long getCompanyID() {
        return companyID;
    }
    public void setCompanyID(long companyID) {
        this.companyID = companyID;
    }

    @Enumerated(EnumType.STRING)
    public CompanyType getCompanyType() {
        return companyType;
    }
    public void setCompanyType(CompanyType companyType) {
        this.companyType = companyType;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

和枚举:

  

public enum CompanyType {A,B,C // TODO}

我想获得在公司“companyName”中工作的所有用户的列表。 请注意用户包含对公司的参考。

我非常确定需要连接查询,但我似乎无法确定语法。

我非常赞赏你的帮助。

谢谢!

1 个答案:

答案 0 :(得分:1)

首先改变从用户到公司的映射到..因为许多用户可以属于一个comapny。

<many-to-one name="company>
</many-to-one>

然后您可以使用以下查询。     “来自用户u left outer join u.company,其中u.company.companyName ='XYZ'”