Hello folks
I am working on a
Spring-Hibernate-Extjs application for some while and stuck at a point.I have configured my classes using hibernate annotations 4.1 release and using a DAO to execute my quries,but I am not able to pass a parameter returned from the UI,to the
Query.
POJO Classes
CandidatesCode:
@JsonAutoDetect
@Entity
@Table(name="HC_RESUME_BANK")
public class Candidates{
private int id;
private String firstName;
private String lastName;
private String email;
private String phone;
private Set<CandidateStatus> candidateStats = new HashSet<CandidateStatus>(0);
@OneToMany //(mappedBy="candidate")
@Fetch(FetchMode.JOIN)
@JoinColumn(name="ResID",nullable=false,referencedColumnName="RID")
@JsonIgnore
public Set<CandidateStatus> getCandidateStats() {
return candidateStats;
}
public void setCandidateStats(Set<CandidateStatus> candidateStats) {
this.candidateStats = candidateStats;
}
@Id
@Column(name="RID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="FirstName")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name="LastName")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Column(name="EmailID")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Column(name="Mobile")
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
CandidateStatusCode:
@JsonAutoDetect
@Entity
@Table(name="HC_REQ_RESUME")
public class CandidateStatus{
private int id;
private String statusTitle;
private Candidates candidate;
private int reqid;
private int resid;
private Requirements requirement;
@ManyToOne
@Fetch(FetchMode.JOIN)
@JoinColumn(name="ReqID",insertable=false,updatable=false)
//@JsonIgnore
public Requirements getRequirement() {
return requirement;
}
public void setRequirement(Requirements requirement) {
this.requirement = requirement;
}
public int getReqid() {
return reqid;
}
public void setReqid(int reqid) {
this.reqid = reqid;
}
public int getResid() {
return resid;
}
public void setResid(int resid) {
this.resid = resid;
}
@ManyToOne //(fetch = FetchType.LAZY,cascade=CascadeType.ALL,targetEntity=Candidates.class)
@Fetch(FetchMode.JOIN)
@JoinColumn(name="ResID",insertable=false,updatable=false)
//@ForeignKey(name = "FK_ResID")
public Candidates getCandidate() {
return candidate;
}
public void setCandidate(Candidates candidate) {
this.candidate = candidate;
}
@Id
@Column(name="RID",insertable=false,updatable=false)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="StatusTitle")
public String getStatusTitle() {
return statusTitle;
}
public void setStatusTitle(String statusTitle) {
this.statusTitle = statusTitle;
}
}
ClientsCode:
@JsonAutoDetect
@Entity
@Table(name="HC_CLIENTS")
public class Clients{
private int id;
private String clientName;
private Set<Requirements> require= new HashSet<Requirements>(0);
//private List<Requirements> require;
@OneToMany //(fetch=FetchType.EAGER)
@Fetch(FetchMode.JOIN)
@JoinColumn(name="ClientID",referencedColumnName="RID")
@JsonIgnore
public Set<Requirements> getRequire() {
return require;
}
public void setRequire(Set<Requirements> require) {
this.require = require;
}
@Id
@Column(name="RID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="ClientName")
public String getClientName() {
return clientName;
}
public void setClientName(String clientName) {
this.clientName = clientName;
}
}
RequirementsCode:
@JsonAutoDetect
@Entity
@Table(name="HC_REQUISITIONS")
public class Requirements{
private int id;
private int clientId;
private String reqTitle;
private Clients client;
@ManyToOne
@Fetch(FetchMode.JOIN)
@JoinColumn(name="ClientID",insertable=false,updatable=false)
public Clients getClient() {
return client;
}
public void setClient(Clients client) {
this.client = client;
}
private Set<CandidateStatus> candidateStatus = new HashSet<CandidateStatus>(0);
/*For candidate status class*/
@OneToMany
@Fetch(FetchMode.JOIN)
@JoinColumn(name="ReqID",referencedColumnName="RID")
@JsonIgnore
public Set<CandidateStatus> getCandidateStatus() {
return candidateStatus;
}
public void setCandidateStatus(Set<CandidateStatus> candidateStatus) {
this.candidateStatus = candidateStatus;
}
@Id
@Column(name="RID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="ClientID")
public int getClientId() {
return clientId;
}
public void setClientId(int clientId) {
this.clientId = clientId;
}
@Column(name="ReqTitle")
public String getReqTitle() {
return reqTitle;
}
public void setReqTitle(String reqTitle) {
this.reqTitle = reqTitle;
}
}
DAO classCode:
@Repository
public class CandidatesDAO implements ICandidatesDAO {
private HibernateTemplate hibernateTemplate;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
hibernateTemplate = new HibernateTemplate(sessionFactory);
}
@SuppressWarnings("unchecked")
@Override
public List<CandidateStatus> getcandidateStatus() {
[b]//return hibernateTemplate.find("select candstat.candidate.firstName as firstName,candstat.candidate.lastName as lastName,candstat.candidate.email as email,candstat.statusTitle as statusTitle,candstat.requirement.client.clientName as clientName,candstat.requirement.reqTitle as reqTitle from CandidateStatus as candstat");[/b]
return hibernateTemplate.find("from CandidateStatus");
}
I have achieved a join using @JoinColumn annotation,but generates
N+1 queries(very annoying performance wise).Also,I want to pass a parameter to the query as
"from CandidateStatuswhere reqTitle='xyz'".I am using Spring controller
Code:
@Controller
public class CandidatesController {
private CandidatesService candidatesService;
@RequestMapping(value="/candidates/view.action")
public @ResponseBody Map<String,? extends Object> view() throws Exception {
try{
List<CandidateStatus> candidatestatus = candidatesService.getCandidateStatusList();
return getMap(candidatestatus);
} catch (Exception e) {
return getModelMapError("Error retrieving Candidates from database.");
}
}
Please help me in solving the N+1 problem and query building for passing dynamic paramters.
Thanks and Regards
Sachin
Software Engineer
http://www.optionsconsultancy.com