0
我有一個複合主鍵。鑰匙又是兩個實體。我想根據主鍵內的實體進行查詢。複合主鍵查詢
我的目的是在主鍵是這樣的
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", [email protected](name="user_id")),
@AssociationOverride(name="pk.project", [email protected](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;
}
}
是的,就是這個屬性。我試着按照你講的方式去做。 Iguess屬性名和別名應保持其他way.ie(projCriteria.createAlias(「projectUsers.pk」,「PK」);生成的查詢,但在「where子句中給出了錯誤 未知列「users3_.EMAIL」 ' – DebD
查詢生成 選擇 this_.PROJECT_ID如PROJECT1_1_1_, this_.CREATED_DATE如CREATED2_1_1_, this_.DESCRIPTION如DESCRIPT3_1_1_, this_.NAME如NAME1_1_, this_.TYPE如TYPE1_1_, projectuse1_.project_id如project2_6_0_, projectuse1_ .user_id如user3_6_0_, projectuse1_.isLead如isLead6_0_ 從 PROJECT_DTLS THIS_ 旅店呃加入 PROJECT_USERS projectuse1_ on this_.PROJECT_ID = projectuse1_.project_id 其中 users3_.EMAIL =? 和projectuse1_.isLead =?它會給出錯誤 – DebD
你能否建議我在哪裏出錯?如果我需要提供更多詳細信息,請告訴我。 – DebD