V.Rohan
V.Rohan

Reputation: 945

how can update DB using hibernate?

I am working on project which uses jsf 1.X and hibernate, where am new. We are using below code to update_

        Shift4DAO dao = Shift4DAO.getInstance();
        Session session = dao.createNewSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.saveOrUpdate(shift4B);
            transaction.commit();
        } catch (final HibernateException e) {
            e.printStackTrace();
            if (transaction != null) {
                transaction.rollback();
            } 

How session.saveOrUpdate(shift4B); it is working(Flow)? where Shift4DAO.java

public class Shift4DAO extends BaseShift4DAO {

public Shift4DAO () {}
}

And Shift4.java

public class Shift4 extends BaseShift4 {
private static final long serialVersionUID = 1L;


public Shift4 () {
    super();
}


public Shift4 (java.lang.Integer id) {
    super(id);
}


public Shift4 (
    java.lang.Integer id,
    org.azureworlds.dao.Employee createdBy,
    org.azureworlds.dao.Employee lastUpdatedBy,
    org.azureworlds.dao.Reservation reservation) {

    super (
        id,
        createdBy,
        lastUpdatedBy,
        reservation);
}

}

I fail to understand how i get connect to update for updating data. is anybody can simplify this, where i need to check or how connecting to HB? Thank u!!!!

Upvotes: 0

Views: 141

Answers (1)

Firo
Firo

Reputation: 30813

dao.createNewSession(); creates a hibernate session which encapsulates a database connection and implements the "Unit of Work" pattern. session.saveOrUpdate(shift4B); tells the session that shift4B should be created(INSERT) or updated depending on the state of the entity (new or changed).

Upvotes: 1

Related Questions