Search code examples
hibernateooad

OO design and hibernate


I have tried to model the job portal use case as in class diagram below. I have made Skill entity as shareable by both job as well as job seeker hoping to reuse.

I have revised my oo design since I am sharing the skill I suppose it means its M:N relation instead of 1:N.

questions:

How can I do below things using hibernate ? do I need to resort to SQL ?

  1. since Skill has M:N relation it will need association table JobSkill and SeekerSkill. how to create job or seeker instance so that they use the existing skill in database ?
  2. I need to do skill matching such that job skill requirement is subset of candidate skills for:
    a) employer e1: find all candidates - 1{job + 1{candidate}N }N
    b) candidate c1: find all jobs - 1{job + employer }N

I am thinking to add a Business Service class for this case as JobPortal with some methods as below pseudo code. If one can answer the HQL query needed for the method findJobsForSeeker:

public class JobPortal {

  public int createEmployer(String name, Address address) {
    Employer e = null;
    HBUtil.create(e = new Employer(name, address));
    return e.getId();
  }

  public void addJobToEmployer(int empid, String jobName, String[][] skills) {
    Employee e = HBUtil.get(empid, Employee.class);
    Job j = new Job(jobName);
    Skill s = null;
    for(int i=0; i<skills.length; i++) {
      s = HBUtil.find(skills[i][0], skills[i][1], Skill.class);
      if (null == s) {
        s = new Skill(skills[0], Interger.parseInt(skills[1])); //name, experience
      }
      j.add(s);

    }
    e.add(j);
    HBUtil.save(e);
  }

  public int createSeeker(String name) {
    Seeker s = null;
    DBUtil.create(s = new Seeker(name));
    return s.getId();
  }

  public void addSkillsToSeeker(int sid, String[][] skills) {
    Seeker seeker = HBUtil.get(sid, Seeker.class);
    for(int i=0; i<skills.length; i++) {
      s = HBUtil.find(skills[i][0], skills[i][1], Skill.class);
      if (null == s) {
        s = new Skill(skills[0], Interger.parseInt(skills[1])); //name, experience
      }
      seeker.add(s);
    }
    HBUtil.save(seeker);
  }


  public void findJobsForSeeker(int sid) {
   //what HQL do use ? 
  }

}

Solution

  • To answer you first question: You search for the skills you want to attach to the new job or seeker (with a HQL query). Once you have found the skills, you add them to the collection of skills of the new job or seeker:

    List<Skill> skills = session.createQuery("select skill from Skill skill where ...")
                                .list();
    for (Skill skill : skills) {
        newJob.addSkill(skill);
    }
    

    I don't understand the syntax used in your second question. If you want to find all seekers who have all the skills in a given set of skills:

    select seeker from Seeker seeker where not exists (
        select skill.id from Skill skill where skill in (:skillSet)
        and skill.id not in (select skill2.id from Seeker seeker2
                             inner join seeker2.skills skill2
                             where seeker2 = seeker))