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.abc.salesinventory.service.newpackage.MasterServiceImpl.java

@Override
public String saveOrUpdateProduct(Product product) throws HibernateException {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();/*  www .j av  a  2  s  . c  o  m*/
    session.saveOrUpdate(product);
    session.getTransaction().commit();
    session.close();
    return product.getProductCode();
}

From source file:com.abc.salesinventory.service.newpackage.MasterServiceImpl.java

@Override
public String saveOrUpdateStock(Stock stock) throws HibernateException {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();/*from   ww  w . j  a  v a  2 s .  co  m*/
    session.saveOrUpdate(stock);
    session.getTransaction().commit();
    session.close();
    return stock.getStockId();
}

From source file:com.abc.salesinventory.service.newpackage.SecurityServiceImpl.java

@Override
public String saveOrUpdateUser(User user) throws HibernateException {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();//ww w  . j av  a2 s .c o m
    session.saveOrUpdate(user);
    session.getTransaction().commit();
    session.close();
    return user.getId();
}

From source file:com.abc.salesinventory.service.newpackage.SecurityServiceImpl.java

@Override
public String saveOrUpdateRole(Role role) throws HibernateException {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();/* w  ww.  j  ava 2s .c  o  m*/
    session.saveOrUpdate(role);
    session.getTransaction().commit();
    session.close();
    return role.getId();
}

From source file:com.abc.salesinventory.service.newpackage.SecurityServiceImpl.java

@Override
public String saveOrUpdatePermission(Permission role) throws HibernateException {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();//ww w .j  a  va 2s. com
    session.saveOrUpdate(role);
    session.getTransaction().commit();
    session.close();
    return role.getId();
}

From source file:com.abiquo.abiserver.commands.impl.VirtualApplianceCommandImpl.java

License:Open Source License

/**
 * Delete a network RASD when deleting a note or virtual appliance.
 * //from  w  ww.  ja v a 2s . c  o  m
 * @param session The hibernate session.
 * @param resourceManagement The resource to delete.
 */
protected void deleteNetworkRasd(final Session session, final ResourceManagementHB resourceManagement) {
    if (resourceManagement instanceof IpPoolManagementHB) {
        IpPoolManagementHB netMan = (IpPoolManagementHB) resourceManagement;

        ResourceAllocationSettingData rasd = netMan.getRasd();
        session.delete(rasd);

        if (resourceManagement.getVirtualDataCenter().getDefaultVlan().getNetworkType()
                .equals(NetworkType.UNMANAGED.name())) {
            session.delete(netMan);
        } else {
            netMan.setRasd(null);
            netMan.setVirtualMachine(null);
            netMan.setVirtualApp(null);
            netMan.setConfigureGateway(Boolean.FALSE);

            if (rasd.getResourceSubType() != null && rasd.getResourceSubType().equalsIgnoreCase("2")) {
                netMan.setMac(null);
                netMan.setVirtualDataCenter(null);
                netMan.setName(null);
            }
            session.saveOrUpdate(netMan);
        }

    }
}

From source file:com.abiquo.abiserver.commands.impl.VirtualApplianceCommandImpl.java

License:Open Source License

/**
 * Delete a storage RASD when removing a node or a virtual appliance.
 * /*from w  w  w  .j  a  v  a 2s . co  m*/
 * @param session The Hibernate session.
 * @param resourceManagement The resource to delete.
 */
protected void deleteStorageRasd(final Session session, final ResourceManagementHB resourceManagement) {
    resourceManagement.setVirtualApp(null);
    resourceManagement.setVirtualMachine(null);
    session.saveOrUpdate(resourceManagement);
}

From source file:com.abiquo.abiserver.model.PopulateModelTest.java

License:Open Source License

/**
 * Create a ResourceAllocatingSettingData of type 10
 * //from   w  w  w . j av a2s.  c  om
 * @param session
 * @throws PersistenceException
 */
private void createRASD10(final Session session) throws PersistenceException {
    session.beginTransaction();

    ResourceAllocationSettingData rasd = new ResourceAllocationSettingData();
    rasd.setInstanceID("3");
    rasd.setAddress("00:1d:09:2c:50:d6");
    rasd.setResourceSubType("PCNet32");
    rasd.setConnection("VM Network");
    rasd.setElementName("Ethernet adapter on 'VM Network'");
    rasd.setResourceType(10);

    session.saveOrUpdate(rasd);

    // add this to the list "rasdHBs"
    rasdHBs.add(rasd);
}

From source file:com.abiquo.abiserver.model.PopulateModelTest.java

License:Open Source License

private <T extends ResourceManagementHB> void createRasdManagement(final Session session,
        final int[] resourceTypes, final Class<T> cl) throws PersistenceException {

    VirtualappHB virtualAppHB = (VirtualappHB) session.get("VirtualappExtendedHB", vApp.getIdVirtualApp());

    VirtualDataCenterHB virtualDataCenterHB = virtualAppHB.getVirtualDataCenterHB();

    Disjunction virtualAppDisjuction = Restrictions.disjunction();
    virtualAppDisjuction.add(Restrictions.eq("idVirtualApp", virtualAppHB.getIdVirtualApp()));
    ArrayList<NodeHB> nodes = (ArrayList<NodeHB>) session.createCriteria(NodeHB.class).add(virtualAppDisjuction)
            .list();//from  ww  w.  j a  v a  2 s.c  om

    VirtualmachineHB virtualMachineHB;

    ArrayList<ResourceAllocationSettingData> rasds;

    ResourceManagementHB resourceManagement = null;
    Timestamp timestamp = new Timestamp(new GregorianCalendar().getTimeInMillis());
    for (NodeHB node : nodes) {
        if (node.getType() == NodeTypeEnum.VIRTUAL_IMAGE) {
            NodeVirtualImageHB nodeVirtualImage = (NodeVirtualImageHB) node;
            virtualMachineHB = nodeVirtualImage.getVirtualMachineHB();

            if (virtualMachineHB != null) {
                for (int resourceType : resourceTypes) {
                    rasds = getRasds(session, resourceType);

                    for (ResourceAllocationSettingData rasd : rasds) {
                        try {
                            resourceManagement = cl.newInstance();
                        } catch (Exception e) {
                            throw new PersistenceException(
                                    "Unable to create a new instance of " + cl.getName());
                        }

                        resourceManagement.setIdResourceType(rasd.getResourceType() + "");
                        resourceManagement.setRasd(rasd);
                        resourceManagement.setVirtualApp(virtualAppHB);
                        resourceManagement.setVirtualMachine(virtualMachineHB);
                        resourceManagement.setVirtualDataCenter(virtualDataCenterHB);

                        session.saveOrUpdate(resourceManagement);

                        resourceManagementHBs.add(resourceManagement);

                    }
                }

            }
        }

    }

}

From source file:com.acentera.utils.ProjectsHelpers.java

License:Open Source License

public static String updateServer(Long projectId, Long serverId, JSONObject updatedServer)
        throws JsonProcessingException {

    SecurityController.checkPermission(projectId, PermisionTagConstants.SERVER, serverId,
            PermissionActionConstats.EDIT);

    ProjectDevices device = DeviceImpl.getProjectDevice(projectId, serverId);

    //Device device = projectDevice.getDevice();

    Session s = HibernateSessionFactory.getSession();

    if (updatedServer.containsKey("tags")) {
        JSONArray listOfTags = updatedServer.getJSONArray("tags");

        final Set<ProjectDevicesTags> tags = device.getTags();
        Set<ProjectDevicesTags> newTags = new HashSet<ProjectDevicesTags>();

        for (ProjectDevicesTags tag : tags) {
            tag.disable();//from w  w  w .  j  ava2  s .  c  o m
        }

        int len = listOfTags.size();
        for (int i = 0; i < len; i++) {
            JSONObject jsoData = listOfTags.getJSONObject(i);
            String data = jsoData.getString("name");

            List<ProjectDevicesTags> foundItem = select(tags,
                    having(on(ProjectDevicesTags.class).getName(), equalTo(data)));

            ProjectDevicesTags projectTags = null;
            if (foundItem.size() == 1) {
                //We found it..
                projectTags = foundItem.get(0);
            } else {
                //This tag is not in this object yet.
                projectTags = new ProjectDevicesTags();
            }
            projectTags.setProjectTags(
                    ProjectTagsImpl.getOrCreateTags(device.getProject(), data, TagConstants.ANY));
            projectTags.enable();

            tags.add(projectTags);
            s.saveOrUpdate(projectTags);
            newTags.add(projectTags);
        }

        //Set the new Tags
        device.setTags(tags);

        s.saveOrUpdate(device);

        //Since we disabled all data now.. lets return the object.
        device.setTags(newTags);

        //Do not return the provider object as someone will potentially call "save on the object"

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        ObjectWriter ow = mapper.writer();
        JSONObject jsoRes = new JSONObject();
        jsoRes.put("servers", ow.writeValueAsString(device));
        //return device;//ProjectsHelpers.getProjectProvidersAsJson(provider);
        return jsoRes.toString();
    }

    return null;
}