title: Trying to test the existence of a record using hibernate 4
body: <p>I have an <code>addEmployee</code> webservice in the Spring 4 controller as shown in the code which is accepting   <code>emp_name</code> and <code>definition</code>. 
  The insertion in the database table is happening using Hibernate 4 when the <code>insertIntegertype</code> method is called. </p>
  
  <pre><code>@RequestMapping(value="/addEmployee", method=RequestMethod.GET)
      public String addEmployee
      (
          @RequestParam(value="name", defaultValue="") String emp_name,   
          @RequestParam(value="definition", defaultValue="") String definition
      ) 
      {  int emp_id = 0;
  
          try {
              EmpDao empDao = (EmpDao)context.getBean("empDao");
              Emp empInsert = new Emp();
  
              empInsert.setName(emp_name+" Attributes");
              empInsert.setDefinition(definition);
              empInsert.setOwnerName(owner_name_);
              emp_id = empDao.insertIntegertype(empInsert);
  
              }catch (Throwable th){
              // some code here 
              }
      }
  </code></pre>
  
  <p><strong>Need to Include the following check:</strong></p>
  
  <p>As mentioned in the code while setting the name of an employee, I am also adding <code>Attributes</code> word in this line <code>empInsert.setName(emp_name+" Attributes");</code> 
  So, there can be a scenario when the same name is already present in the database and in that scenario, I would like to test 
  whether that name already exists in the database or not and then only proceed with the insert. 
  I am wondering is there a way in hibernate which can help me in figuring out this thing?</p>
  
  <p>In case needed, my <code>insertIntegertype</code> hibernate method is defined as follows   </p>
  
  <pre><code>public int insertIntegertype(Emp emp)
      {
          logger.debug("Starting EmpDaoImpl.insert() .....");
          Session session = null;
          Transaction tx = null;
          boolean status = true;
          int Emp_id = 0;
          try {
              session = sessionFactory.openSession();
              tx = session.beginTransaction();
              session.persist(emp);
              tx.commit();
              Emp_id = emp.getEmpId();
          } catch(Exception ex) {
              tx.rollback();
              ex.printStackTrace();
              status = false;
          } finally {
              session.close();
          }
          logger.debug("Completed EmpDaoImpl.insert() .....");
          return Emp_id;
      }       
  </code></pre>