复合主键查询

时间:2012-11-08 20:53:32

标签: hibernate hibernate-criteria composite-primary-key

我有一个复合主键。密钥又是两个实体。我想基于主键内的实体进行查询。

我的目的是查询主键这样的东西

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

但我得到一个例外 无法解析属性:pk.users.email: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;
    }

}

1 个答案:

答案 0 :(得分:0)

您没有提供UserDetailsRO类,因此我们假设其中的'email'属性是属性注释的。如果是这种情况,请尝试在“条件”中创建别名,直至“用户”。类似的东西:

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));