-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Passing parameter to query in a Spring-Hibernate application
PostPosted: Sat Mar 24, 2012 3:07 pm 
Newbie

Joined: Wed Jan 25, 2012 1:59 am
Posts: 8
Hello folks
I am working on aSpring-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
Candidates
Code:
@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;
   }
   
}

CandidateStatus
Code:
@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;
   }
   
}

Clients
Code:
@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;
   }
   
   
}

Requirements
Code:
@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 class
Code:
@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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.