Dan
Dan

Reputation: 463

Trying to test the existence of a record using hibernate 4

I have an addEmployee webservice in the Spring 4 controller as shown in the code which is accepting emp_name and definition. The insertion in the database table is happening using Hibernate 4 when the insertIntegertype method is called.

@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 
            }
    }

Need to Include the following check:

As mentioned in the code while setting the name of an employee, I am also adding Attributes word in this line empInsert.setName(emp_name+" Attributes"); 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?

In case needed, my insertIntegertype hibernate method is defined as follows

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;
    }       

Upvotes: 0

Views: 57

Answers (2)

Mazen Embaby
Mazen Embaby

Reputation: 1361

Approach : Make a selection query from database with a like "Name%" if not exist make the Insertion

You have 3 options :

1-Using Native Query

Session session = sessionFactory.openSession();
session.beginTransaction();
String sql = "select First_Name FROM employee where First_Name like 'NAME%'"
NativeQuery query = session.createNativeQuery(sql);
List<Object[]> list = query.list();
if (list.isEmpty()) {
session.persist(emp);
}

2-Using HQL

Session session = sessionFactory.openSession();
session.beginTransaction();
Query query = session.createQuery("select u.name from Employee u where u.name like :Name");
List<Object[]> list= query.setParameter("Name", name+ "%").list();
if (list.isEmpty()) {
session.persist(emp);
}

3-Using CriteriaBuilder

Session session = sessionFactory.openSession();
session.beginTransaction();
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<String> criteria = builder.createQuery(String.class);
Root  root = criteria.from(class);
Path <String> attribute = root.get(col);
criteria.select(attribute).where(builder.(attribute,NAME + "%"));
List<String> list = session.createQuery(criteria).getResultList();
if (list.isEmpty()) {
session.persist(emp);
}

Upvotes: 1

yevgeniy.batulin
yevgeniy.batulin

Reputation: 186

You should query the database to check if an employee with such name exists. Then you can proceed with saving the employee according to your requirements

Upvotes: 0

Related Questions