Example usage for org.hibernate Criteria uniqueResult

List of usage examples for org.hibernate Criteria uniqueResult

Introduction

In this page you can find the example usage for org.hibernate Criteria uniqueResult.

Prototype

public Object uniqueResult() throws HibernateException;

Source Link

Document

Convenience method to return a single instance that matches the query, or null if the query returns no results.

Usage

From source file:com.abiquo.server.core.infrastructure.storage.StoragePoolDAO.java

License:Open Source License

public StoragePool findPoolByName(final Integer deviceId, final String name) {
    Criteria criteria = createCriteria(Restrictions.eq("device.id", deviceId))
            .add(Restrictions.eq("name", name));
    Object obj = criteria.uniqueResult();
    return (StoragePool) obj;
}

From source file:com.abiquo.server.core.infrastructure.storage.TierDAO.java

License:Open Source License

public Tier getTierById(final Integer datacenterId, final Integer tierId) {

    Criteria criteria = createCriteria(Restrictions.eq("datacenter.id", datacenterId))
            .add(Restrictions.eq("id", tierId));
    Object obj = criteria.uniqueResult();
    return (Tier) obj;
}

From source file:com.abiquo.server.core.infrastructure.storage.VolumeManagementDAO.java

License:Open Source License

public VolumeManagement getVolumeFromImage(final Integer idImage) {
    Criteria criteria = createCriteria(Restrictions.eq(
            VolumeManagement.VIRTUAL_MACHINE_TEMPLATE_PROPERTY + "." + PersistentEntity.ID_PROPERTY, idImage),
            Restrictions.isNull(RasdManagement.TEMPORAL_PROPERTY));
    Object obj = criteria.uniqueResult();
    return (VolumeManagement) obj;
}

From source file:com.abiquo.server.core.scheduler.FitPolicyRuleDAO.java

License:Open Source License

/**
 * Returns the {@link FitPolicyRuleHB} that applies to the given datacenter.
 * //from www .j a v a2s  .c o m
 * @param idDatacenter The id of the datacenter.
 * @return The <code>FitPolicyRuleHB</code> that applies to the given datacenter.
 */
public FitPolicyRule getFitPolicyForDatacenter(final Integer idDatacenter) {
    Criteria crit = createNestedCriteria(FitPolicyRule.DATACENTER_PROPERTY);
    crit.add(Restrictions.eq(Datacenter.ID_PROPERTY, idDatacenter));

    return (FitPolicyRule) crit.uniqueResult();
}

From source file:com.abiquo.server.core.statistics.CloudUsageDAO.java

License:Open Source License

public CloudUsage sumTotalCloudUsage() {
    // TODO: Include aggregators functionality in bzengine?
    Session ses = HibernateEntityManagerHelper.getSession(getEntityManager());

    Criteria crit = ses.createCriteria(CloudUsage.class);
    ProjectionList proList = Projections.projectionList();
    proList.add(Projections.sum(CloudUsage.SERVERS_TOTAL_PROPERTY));
    proList.add(Projections.sum(CloudUsage.SERVERS_RUNNING_PROPERTY));
    proList.add(Projections.sum(CloudUsage.STORAGE_TOTAL_PROPERTY));
    // proList.add(Projections.sum(CloudUsage.STORAGE_RESERVED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.STORAGE_USED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.PUBLIC_I_PS_TOTAL_PROPERTY));
    // proList.add(Projections.sum(CloudUsage.PUBLIC_I_PS_RESERVED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.PUBLIC_I_PS_USED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_MACHINES_TOTAL_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_MACHINES_RUNNING_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_CPU_TOTAL_PROPERTY));
    // proList.add(Projections.sum(CloudUsage.V_CPU_RESERVED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_CPU_USED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_MEMORY_TOTAL_PROPERTY));
    // proList.add(Projections.sum(CloudUsage.V_MEMORY_RESERVED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_MEMORY_USED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_STORAGE_TOTAL_PROPERTY));
    // proList.add(Projections.sum(CloudUsage.V_STORAGE_RESERVED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_STORAGE_USED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.NUM_USERS_CREATED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.NUM_VDC_CREATED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.NUM_ENTERPRISES_CREATED_PROPERTY));

    crit.setProjection(proList);/*from  ww w.  jav a  2  s . c  o  m*/
    Object[] obj = (Object[]) crit.uniqueResult(); // Returns Object[] ->
    CloudUsage result = new CloudUsage();

    int cont = 0;
    result.setServersTotal((Long) obj[cont++]);
    result.setServersRunning((Long) obj[cont++]);
    result.setStorageTotal((Long) obj[cont++]);
    // result.setStorageReserved((Long) obj[cont++]);
    result.setStorageUsed((Long) obj[cont++]);
    result.setPublicIPsTotal((Long) obj[cont++]);
    // result.setPublicIPsReserved((Long) obj[cont++]);
    result.setPublicIPsUsed((Long) obj[cont++]);
    result.setVirtualMachinesTotal((Long) obj[cont++]);
    result.setVirtualMachinesRunning((Long) obj[cont++]);
    result.setVirtualCpuTotal((Long) obj[cont++]);
    // result.setVirtualCpuReserved((Long) obj[cont++]);
    result.setVirtualCpuUsed((Long) obj[cont++]);
    result.setVirtualMemoryTotal((Long) obj[cont++]);
    // result.setVirtualMemoryReserved((Long) obj[cont++]);
    result.setVirtualMemoryUsed((Long) obj[cont++]);
    result.setVirtualStorageTotal((Long) obj[cont++]);
    // result.setVirtualStorageReserved((Long) obj[cont++]);
    result.setVirtualStorageUsed((Long) obj[cont++]);
    result.setNumUsersCreated((Long) obj[cont++]);
    result.setNumVdcCreated((Long) obj[cont++]);
    result.setNumEnterprisesCreated((Long) obj[cont++]);

    return result;
}

From source file:com.actop.model.PermissionManagement.java

public Interfaces saveInterface(String interfaceName) {
    Session s = Connection.getSessionFactory().openSession();
    Criteria c = s.createCriteria(Interfaces.class);
    c.add(Restrictions.eq("interfaceName", convertToBytes(interfaceName)));
    Interfaces checkinterface = (Interfaces) c.uniqueResult();
    if (checkinterface == null) {
        Transaction t = s.beginTransaction();
        Interfaces i = new Interfaces();
        try {//www.  jav  a2s.c om
            i.setInterfaceName(convertToBytes(interfaceName));
            s.save(i);
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
        t.commit();
        return i;
    }
    s.flush();
    s.close();
    return checkinterface;
}

From source file:com.actop.model.PermissionManagement.java

public Interfaces loadInterface(String interfaceName) {
    Session s = Connection.getSessionFactory().openSession();
    Criteria c = s.createCriteria(Interfaces.class);
    c.add(Restrictions.eq("interfaceName", convertToBytes(interfaceName)));
    Interfaces i = (Interfaces) c.uniqueResult();
    s.flush();/*from ww  w.j  a  v  a  2s.  c  o m*/
    s.close();
    return i;
}

From source file:com.actop.model.PermissionManagement.java

public boolean loadPermission(UserLogin ul, Interfaces i) {
    Session s = Connection.getSessionFactory().openSession();
    Criteria c = s.createCriteria(InterfacesHasUserLogin.class);
    c.add(Restrictions.eq("interfaces", i));
    c.add(Restrictions.eq("userLogin", ul));
    InterfacesHasUserLogin ihul = (InterfacesHasUserLogin) c.uniqueResult();
    return ihul != null;
}

From source file:com.actop.model.PromotionManagement.java

public Promotions promoteEmployer(int pid, String promotBy, Date approvedDate, String promotionApprovedBy,
        Date effectiveDate, String promotionStates) {
    Session s = Connection.getSessionFactory().openSession();
    Promotions p = (Promotions) s.load(Promotions.class, pid);
    Transaction t = s.beginTransaction();
    try {/*w ww  . j a v a  2s . c  o  m*/
        Criteria c = s.createCriteria(DepartmentsHasDesignation.class);
        c.add(Restrictions.eq("employers", p.getEmployers()));
        DepartmentsHasDesignation dhd = (DepartmentsHasDesignation) c.uniqueResult();
        Criteria c1 = s.createCriteria(Designation.class);
        c1.add(Restrictions.eq("designation", p.getPromotionFor()));
        Designation d = (Designation) c.uniqueResult();
        dhd.setDesignation(d);
        s.update(dhd);
        p.setPromotBy(convertToBytes(promotBy));
        //            p.setPromotionApproved(approvedDate);
        //            p.setPromotionApprovedBy(convertToBytes(promotionApprovedBy));
        p.setPromotionEffectiveDate(effectiveDate);
        p.setPromotionStates(convertToBytes(promotionStates));
        s.update(p);
    } catch (Exception e) {
        t.rollback();
    }
    t.commit();
    s.flush();
    s.close();
    return p;
}

From source file:com.actop.model.UserManagement.java

public UserLogin loadUserLoginfromEmployer(Employers emp) {
    Session s = Connection.getSessionFactory().openSession();
    Criteria c = s.createCriteria(UserLogin.class);
    c.add(Restrictions.eq("employers", emp));
    UserLogin ul = (UserLogin) c.uniqueResult();
    return ul;// w w  w  . ja v  a 2s .  c  o m
}