0

I have a composite primary key. The keys are in turn two entities. I want to query based on the entities inside the primary key.

My intention is to query on the primary key something like this

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

But I get an exception could not resolve property: pk.users.email of: com.ett.repository.ProjectUsers

I want to query based on the user(of PK class). How to give Alias for the entity which is inside the PK class(UserDetailsRO). PLEASE HELP..!!

The entities and the relations are as below

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

}
DebD
  • 374
  • 1
  • 4
  • 18

1 Answers1

0

You don't provide the UserDetailsRO class, so we'll assume that the 'email' property in it is property annotized. If that's the case, try creating aliases in your Criteria down to the 'users'. Something like:

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));
dlgrasse
  • 308
  • 1
  • 2
  • 10
  • Yes thats the property. I tried doing it the way you told. Iguess the property names and the aliases should be kept the other way.i.e(projCriteria.createAlias("projectUsers.pk", "pk"); The query is generated but gives error Unknown column 'users3_.EMAIL' in 'where clause' – DebD Nov 08 '12 at 21:41
  • Query generated select this_.PROJECT_ID as PROJECT1_1_1_, this_.CREATED_DATE as CREATED2_1_1_, this_.DESCRIPTION as DESCRIPT3_1_1_, this_.NAME as NAME1_1_, this_.TYPE as TYPE1_1_, projectuse1_.project_id as project2_6_0_, projectuse1_.user_id as user3_6_0_, projectuse1_.isLead as isLead6_0_ from PROJECT_DTLS this_ inner join PROJECT_USERS projectuse1_ on this_.PROJECT_ID=projectuse1_.project_id where users3_.EMAIL=? and projectuse1_.isLead=? and it gives error – DebD Nov 08 '12 at 21:42
  • Could you please suggest me where I am making mistake? Please let me know if I need to provide any more details. – DebD Nov 09 '12 at 05:58
  • that sql query looks flawed...the 'where' clause is referencing a table with alias 'users3_', but there isn't one declared in the 'from'/'join' clauses. is the users table mapped in Hibernate? – dlgrasse Nov 09 '12 at 14:09
  • Yes that user table is mapped. I need to query based on the user Name which is inside the UserDetailsRO. – DebD Nov 13 '12 at 01:33