0

我有一个复合主键。键又是两个实体。我想根据主键内的实体进行查询。

我的意图是查询像这样的主键

Criteria projCriteria = session.createCriteria(ProjectDetailsRO.class);
        projCriteria.createAlias("projectUsers", "projectUsers");
        projCriteria.add(Restrictions.eq("projectUsers.pk.users.email", leadUserEmail));

但我得到一个异常无法解析属性: pk.users.email of: com.ett.repository.ProjectUsers

我想根据用户(PK类)进行查询。如何为 PK 类 (UserDetailsRO) 中的实体提供别名。请帮忙..!!

实体和关系如下

@Entity
@Table(name="PROJECT_USERS")
@AssociationOverrides({
@AssociationOverride(name="pk.users", joinColumns=@JoinColumn(name="user_id")), 
@AssociationOverride(name="pk.project", joinColumns=@JoinColumn(name="project_id"))
})
public class ProjectUsers {

    private ProjectUserPK pk= new ProjectUserPK();




    @EmbeddedId
    public ProjectUserPK getPk() {
        return pk;
    }

    public void setPk(ProjectUserPK pk) {
        this.pk = pk;
    }

    @Transient
    public ProjectDetailsRO getProjects() {
        return getPk().getProject();
    }

    public void setProjects(ProjectDetailsRO projects) {
        getPk().setProject(projects);
    }


    @Transient
    public UserDetailsRO getUsers() {
        return getPk().getUsers();
    }

    public void setUsers(UserDetailsRO users) {
        getPk().setUsers(users);
    }



    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((isLead == null) ? 0 : isLead.hashCode());
        result = prime * result + ((pk == null) ? 0 : pk.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ProjectUsers other = (ProjectUsers) obj;
        if (isLead == null) {
            if (other.isLead != null)
                return false;
        } else if (!isLead.equals(other.isLead))
            return false;
        if (pk == null) {
            if (other.pk != null)
                return false;
        } else if (!pk.equals(other.pk))
            return false;
        return true;
    }



}

THE PRIMARY KEY CLASS
----------------------

@Embeddable
public class ProjectUserPK implements Serializable{

    private ProjectDetailsRO project;

    private UserDetailsRO users;

    @ManyToOne
    public ProjectDetailsRO getProject() {
        return project;
    }

    public void setProject(ProjectDetailsRO project) {
        this.project = project;
    }


    @ManyToOne
    public UserDetailsRO getUsers() {
        return users;
    }

    public void setUsers(UserDetailsRO users) {
        this.users = users;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((project == null) ? 0 : project.hashCode());
        result = prime * result + ((users == null) ? 0 : users.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ProjectUserPK other = (ProjectUserPK) obj;
        if (project == null) {
            if (other.project != null)
                return false;
        } else if (!project.equals(other.project))
            return false;
        if (users == null) {
            if (other.users != null)
                return false;
        } else if (!users.equals(other.users))
            return false;
        return true;
    }


}

The Main ENtity
=====================


@Entity
@Table(name="PROJECT_DTLS")
public class ProjectDetailsRO {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="PROJECT_ID")
    private Long projectId;


    @OneToMany(fetch=FetchType.LAZY, mappedBy="pk.project", cascade=CascadeType.ALL)
    private List<ProjectUsers>projectUsers = new LinkedList<ProjectUsers>();




    public void addUser(UserDetailsRO user,  Character lead){
        ProjectUsers association = new ProjectUsers();
        association.setUsers(user);
        association.setProjects(this);
        association.setIsLead(lead);

        this.projectUsers.add(association);
        user.getProjectUsers().add(association);
    }

    public List<ProjectUsers> getProjectUsers() {
        return projectUsers;
    }

    public void setProjectUsers(List<ProjectUsers> projectUsers) {
        this.projectUsers = projectUsers;
    }

}
4

1 回答 1

0

您不提供 UserDetailsRO 类,因此我们假设其中的“电子邮件”属性是属性注释的。如果是这种情况,请尝试在您的标准中创建别名,直至“用户”。就像是:

Criteria projCriteria = session.createCriteria(ProjectDetailsRO.class);
projCriteria.createAlias("projectUsers", "projectUsers");
projCriteria.createAlias("pk", "projectUsers.pk");
projCriteria.createAlias("users", "pk.users");
projCriteria.add(Restrictions.eq("users.email", leadUserEmail));
于 2012-11-08T21:03:03.910 回答