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:com.npower.dm.hibernate.management.ModelManagementBeanImpl.java

License:Open Source License

public void update(ModelCharacter character) throws DMException {
    if (character == null) {
        throw new NullPointerException("Could not add a null ModelCharacter into database.");
    }/*from   w ww . j ava 2s.c o m*/
    Session session = this.getHibernateSession();
    try {
        session.saveOrUpdate(character);
    } catch (HibernateException e) {
        throw new DMException(e);
    } finally {
        if (session != null) {
        }
    }
}

From source file:com.npower.dm.hibernate.management.OTAClientProvJobBeanImpl.java

License:Open Source License

public void disable(long jobID) throws DMException {
    String newState = ProvisionJob.JOB_STATE_DISABLE;
    try {/*from w  ww.  j av a2  s.c  o  m*/
        ProvisionJob job = this.getJobById(jobID);
        job.setLastUpdatedTime(new Date());
        job.setState(newState);
        Session session = this.getHibernateSession();
        session.saveOrUpdate(job);
    } catch (HibernateException e) {
        throw new DMException(e);
    }
}

From source file:com.npower.dm.hibernate.management.OTAClientProvJobBeanImpl.java

License:Open Source License

public void enable(long jobID) throws DMException {
    String newState = ProvisionJob.JOB_STATE_APPLIED;
    try {/* w w w .j  ava  2s  .co m*/
        ProvisionJob job = this.getJobById(jobID);
        job.setState(newState);
        job.setLastUpdatedTime(new Date());
        Session session = this.getHibernateSession();
        session.saveOrUpdate(job);
    } catch (HibernateException e) {
        throw new DMException(e);
    }
}

From source file:com.npower.dm.hibernate.management.OTAClientProvJobBeanImpl.java

License:Open Source License

public void update(ProvisionJob job) throws DMException {
    try {/*from  w  ww.  j  a  v  a 2 s  . c om*/
        Session session = this.getHibernateSession();
        job.setLastUpdatedTime(new Date());
        session.saveOrUpdate(job);
    } catch (HibernateException e) {
        throw new DMException(e);
    }
}

From source file:com.npower.dm.hibernate.management.OTAClientProvJobBeanImpl.java

License:Open Source License

public void update(ClientProvJobTargetDevice targetDevice) throws DMException {
    try {/*from w  w  w .  j  a v  a 2 s  .c  o  m*/
        Session session = this.getHibernateSession();
        targetDevice.setLastUpdatedTime(new Date());
        session.saveOrUpdate(targetDevice);

        // , 
        if (targetDevice.isFinished()) {
            ProvisionJob job = targetDevice.getJob();
            for (ClientProvJobTargetDevice target : job.getOtaTargetDevices()) {
                if (!target.isFinished()) {
                    return;
                }
            }
            job.setState(ProvisionJob.JOB_STATE_FINISHED);
            this.update(job);
        }
    } catch (HibernateException e) {
        throw new DMException(e);
    }
}

From source file:com.npower.dm.hibernate.management.ProfileAssignmentManagementBeanImpl.java

License:Open Source License

public void update(ProfileAssignment assignment) throws DMException {
    if (assignment == null) {
        throw new NullPointerException("Could not add a null ProfileAssignmentEntity into database.");
    }/*ww w  .j ava 2  s .  c  o  m*/
    Session session = this.getHibernateSession();
    try {
        ProfileConfig profile = assignment.getProfileConfig();

        ProfileTemplate template = profile.getProfileTemplate();
        Device device = assignment.getDevice();
        Model model = device.getModel();

        ProfileMapping mapping = model.getProfileMap(template);
        if (mapping == null) {
            // TODO uncomments the following line, make sure the device had a ProfileMapping for this template.
            //throw new DMException("Could not assign the profile to this device, no ProfileMapping defined for this device.");
        }

        long id = assignment.getID();
        if (id == 0) {
            // is new!
            // Automaticly caculate assignIndex

            Query query = session.createQuery("select max(assignmentIndex) from ProfileAssignmentEntity "
                    + " where DEVICE_ID=? and PROFILE_ID=?");
            query.setLong(0, device.getID());
            query.setLong(1, profile.getID());

            Long assignmentIndex = (Long) query.uniqueResult();
            if (assignmentIndex == null) {
                assignment.setAssignmentIndex(1L);
            } else {
                assignment.setAssignmentIndex(assignmentIndex.longValue() + 1);
            }
        }

        // TODO checking this assignment before update(), make sure every attributes has been filled value and match the policy defined by ProfileTemplate
        session.saveOrUpdate(assignment);
    } catch (HibernateException e) {
        throw new DMException(e);
    } finally {
    }
}