0

我是新来的冬眠。

我有以下课程:

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

}

和枚举:

公共枚举 CompanyType { A,B,C //TODO }

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

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

我会非常感谢你的帮助。

谢谢!

4

1 回答 1

1

首先,将用户到公司的映射更改为..因为许多用户可以属于一个公司。

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

然后您可以使用以下查询。“从用户 u 离开外部加入 u.company,其中 u.company.companyName='XYZ'”

于 2012-04-13T14:38:55.303 回答