Search code examples
javahibernatecriterianhibernate-criteria

how to update an object in Hibernate Criteria without knowing it's id


I have this query that I would like to translate in Criteria Hibernate:

UPDATE Patient SET HourOut = ? WHERE IDQueue = ? AND TicketNumber = ? AND DATE_FORMAT(DATE(LastVisitDate), '%Y-%m-%d') = ?;

I understand the way to update an object when it's id is known but I can't figure out how to do it with the kind of where i have here.


Solution

  • You can use a Example.

    Patient example = new Patient();
    example.setLastVisitDate(date);
    example.setIDQueue(idq);
    example.setTicketNumber(tnbr);
    
    matchesDate = session.createCriteria(Patient.class)
                     .add(Example.create(example)).list();
    for(Patient patient : matchesDate){
        Transaction t=session.beginTransaction();
        patient.setHourOut(outH);
        session.saveOrUpdate(patient);
        t.commit();
    }