Example usage for org.hibernate Session saveOrUpdate

List of usage examples for org.hibernate Session saveOrUpdate

Introduction

In this page you can find the example usage for org.hibernate Session saveOrUpdate.

Prototype

void saveOrUpdate(Object object);

Source Link

Document

Either #save(Object) or #update(Object) the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking).

Usage

From source file:ch.icclab.cyclops.persistence.HibernateClient.java

License:Open Source License

/**
 * Let Hibernate store provided object to database
 * @param obj to be stored/* ww  w .  ja  va2 s  .  com*/
 * @return updated object
 */
public Object persistObject(Object obj) {

    // first get session
    Session session = obtainSession();

    // start transaction
    session.beginTransaction();

    // persist object
    session.saveOrUpdate(obj);

    // commit it
    session.getTransaction().commit();

    // close connection
    session.flush();
    session.close();

    return obj;
}

From source file:chem.figures.persist.AtomModel.java

@Override
public void save(Session session, int documentId) {
    // If id is -1, we're saving for the first time, set docId
    if (id == -1 || documentId == -1) {
        this.documentId = documentId;
        session.beginTransaction();/*  www.j a v  a 2 s.c  om*/
        session.save(this);
        session.getTransaction().commit();
    } else {
        // Otherwise, pull from database and refresh
        Query query = session.createQuery("from AtomModel a where a.id = " + id);
        Object obj = query.list().get(0);

        AtomModel persAtom = (AtomModel) obj;
        persAtom.x = x;
        persAtom.y = y;
        persAtom.type = type;

        session.beginTransaction();
        session.saveOrUpdate(persAtom);
        session.getTransaction().commit();
    }
}

From source file:chem.figures.persist.DocumentModel.java

@Override
public void save(Session session, int documentId) {
    // Set timestamp to current time
    this.timestamp = new Date();
    this.id = documentId;

    // If id is -1, we're saving for the first time
    if (id == -1) {
        session.beginTransaction();/*from   w  ww  .  j  a va 2  s . com*/
        session.save(this);
        session.getTransaction().commit();
    } else {
        // Otherwise, pull from database and refresh
        Query query = session.createQuery("from DocumentModel d where d.id = " + id);
        Object doc = query.list().get(0);

        DocumentModel persDoc = (DocumentModel) doc;
        persDoc.setName(name);
        persDoc.setTimestamp(timestamp);

        session.beginTransaction();
        session.saveOrUpdate(persDoc);
        session.getTransaction().commit();
    }
}

From source file:chem.figures.persist.ElectronModel.java

@Override
public void save(Session session, int documentId) {
    // If id is -1, we're saving for the first time, set docId
    if (id == -1 || documentId == -1) {
        id = -1;/* w ww . j av a 2 s.c om*/
        session.beginTransaction();
        session.save(this);
        session.getTransaction().commit();
    } else {
        // Otherwise, pull from database and refresh
        Query query = session.createQuery("from ElectronModel e where e.id = " + id);
        Object obj = query.list().get(0);

        ElectronModel persEl = (ElectronModel) obj;
        persEl.angle = angle;

        session.beginTransaction();
        session.saveOrUpdate(persEl);
        session.getTransaction().commit();
    }
}

From source file:chiron.maxscore.dao.impl.BaseDAOImpl.java

/**
 * ?/*from w w w  .  j a v a 2s  . c o  m*/
 *
 * @param e 
 */
@Override
public void save(E e) {
    Session session = sessionFactory.getCurrentSession();
    session.saveOrUpdate(e);
}

From source file:cimitero.rest.BodyRESTService.java

@POST
public ResponseDto updateBody(BodyDto bodyDto) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from  w  ww  .  ja v a2 s.  co m
    TBody tmpBody = new TBody(bodyDto.getPersonId(), bodyDto.getTitle(), bodyDto.getFirstName(),
            bodyDto.getLastName(), bodyDto.getOtherFirstNames(), bodyDto.getGebDatum(), bodyDto.getAddress(),
            bodyDto.getTelephoneNumber(), bodyDto.getDateOfDeath());
    TTomb tmpTomb = (TTomb) session.get(TTomb.class, bodyDto.getTombId());
    tmpBody.setTomb(tmpTomb);
    if (bodyDto.getPersonId() == -1)
        tmpBody.setPersonId(null);
    session.saveOrUpdate(tmpBody);
    ResponseDto response = new ResponseDto(true);
    session.getTransaction().commit();
    return response;
}

From source file:cimitero.rest.CemetryGroundRESTService.java

@POST
public ResponseDto updateCemetryGround(CemetryGroundDto cemetryGroundDto) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();// w w  w .j a  v a2  s.  co m
    TCemetryGround tmpCemetryGround = new TCemetryGround(cemetryGroundDto.getCemetryGroundId(),
            cemetryGroundDto.getName(), cemetryGroundDto.getAddress());
    TCemetry tmpCemetry = (TCemetry) session.get(TCemetry.class, cemetryGroundDto.getCemetryId());
    tmpCemetryGround.setCemetry(tmpCemetry);
    if (cemetryGroundDto.getCemetryGroundId() == -1)
        tmpCemetryGround.setCemetryGroundId(null);
    session.saveOrUpdate(tmpCemetryGround);
    ResponseDto response = new ResponseDto(true);
    session.getTransaction().commit();
    return response;
}

From source file:cimitero.rest.CemetryRESTService.java

@POST
public ResponseDto updateCemetry(TCemetry cemetry) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from  w ww.ja  va2s .  c  om
    if (cemetry.getCemetryId() == -1)
        cemetry.setCemetryId(null);
    session.saveOrUpdate(cemetry);
    ResponseDto response = new ResponseDto(true);
    session.getTransaction().commit();
    return response;
}

From source file:cimitero.rest.CoordinateRESTService.java

@POST
public ResponseDto updateCoordinate(TCoordinate coordinate) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*  www. j  a  v a2s  .co m*/
    if (coordinate.getCoordinateId() == -1)
        coordinate.setCoordinateId(null);
    session.saveOrUpdate(coordinate);
    ResponseDto response = new ResponseDto(true);
    session.getTransaction().commit();
    return response;
}

From source file:cimitero.rest.CustomerRESTService.java

@POST
public ResponseDto updateCustomer(CustomerDto customerDto) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from  w  w w. jav  a  2 s  . c om
    if (customerDto.getGebDatum() != null) {
        customerDto.setGebDatum(new Date(customerDto.getGebDatum().getTime()));
    }
    TCustomer tmpCustomer = new TCustomer(customerDto.getPersonId(), customerDto.getTitle(),
            customerDto.getFirstName(), customerDto.getLastName(), customerDto.getOtherFirstNames(),
            customerDto.getGebDatum(), customerDto.getAddress(), customerDto.getTelephoneNumber());
    List<TTomb> tmpTombs = new ArrayList<TTomb>();
    if (customerDto.getTombIds() != null) {
        for (Integer tombId : customerDto.getTombIds()) {
            TTomb tmpTomb = (TTomb) session.get(TTomb.class, tombId);
            if (tmpTomb != null)
                tmpTombs.add(tmpTomb);
        }
    }
    //if(tmpTombs.size() > 0 && customerDto.getTombIds() != null) tmpCustomer.setTombs(tmpTombs);
    if (customerDto.getPersonId() == -1)
        tmpCustomer.setPersonId(null);
    session.saveOrUpdate(tmpCustomer);
    ResponseDto response = new ResponseDto(true);
    session.getTransaction().commit();
    return response;
}