Example usage for org.hibernate Query uniqueResult

List of usage examples for org.hibernate Query uniqueResult

Introduction

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

Prototype

R uniqueResult();

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.abiserver.abicloudws.InfrastructureWS.java

License:Open Source License

@Override
public BasicResult forceRefreshVirtualMachineState(final VirtualMachine virtualMachine) {
    logger.info("Forcing refresh of the virtual machine state: {}", virtualMachine.getId());
    BasicResult result = new BasicResult();
    VirtualappHB virtualappHBPojo = null;
    Session session = null;//w w  w .  j  a  va2s . c om
    Transaction transaction = null;
    try {
        session = HibernateUtil.getSession();
        transaction = session.beginTransaction();
        Query query = session.createSQLQuery(IDVIRTUALAPP_SQL_BY_VM);
        query.setString("id", virtualMachine.getId().toString());
        Integer virtualApplianceId = (Integer) query.uniqueResult();
        virtualappHBPojo = (VirtualappHB) session.get("VirtualappExtendedHB", virtualApplianceId);
        VirtualAppliance vapp = virtualappHBPojo.toPojo();
        String virtualSystemMonitorAddress = RemoteServiceUtils.getVirtualSystemMonitorFromVA(vapp);
        // EventingSupport.subscribePullEventToVM(virtualMachine, virtualSystemMonitorAddress);
    } catch (PersistenceException e) {
        logger.trace("An error occurred when retrieving the VirtualSystemMonitor", e.getStackTrace()[0]);
    } catch (RemoteServiceException e) {
        logger.trace("An error occurred when contacting the VirtualSystemMonitor", e.getStackTrace()[0]);
    }

    result.setSuccess(true);
    return result;
}

From source file:com.abiquo.abiserver.business.authentication.AuthenticationManagerDB.java

License:Open Source License

/**
 * gets a user from DB given the credentials.
 * //w ww  .  j a v a2s  .c om
 * @param login The user credentials.
 * @param session The Hibernate session.
 * @return The user.
 */
private UserHB getUser(final Login login, final Session session) {
    // Checking if a user exists for the given credentials (remeber to check that the user
    String customQuery = "SELECT * FROM user WHERE user= :user AND password= :password";

    Query query = session.createSQLQuery(customQuery).addEntity(UserHB.class);
    query.setString("user", login.getUser());
    query.setString("password", login.getPassword());
    return (UserHB) query.uniqueResult();
}

From source file:com.abiquo.abiserver.business.authentication.AuthenticationManagerDB.java

License:Open Source License

/**
 * Gets the user from DB using the authentication Token information.
 * //from w ww  .j  ava  2s .  c  o m
 * @param login The object with the token information.
 * @param session The Hibernate session.
 * @return The user.
 */
private UserHB getUserUsingToken(final Login login, final Session session) throws Exception {
    // Get token data
    String[] token = TokenUtils.getTokenFields(login.getAuthToken());
    String tokenUser = TokenUtils.getTokenUser(token);
    long tokenExpiration = TokenUtils.getTokenExpiration(token);
    String tokenSignature = TokenUtils.getTokenSignature(token);

    // Check token expiration
    if (TokenUtils.isTokenExpired(tokenExpiration)) {
        throw new Exception("Authentication token has expired.");
    }

    // Get the token user from db
    String customQuery = "SELECT * FROM user WHERE user= :user";
    Query query = session.createSQLQuery(customQuery).addEntity(UserHB.class);
    query.setString("user", tokenUser);
    UserHB userHB = (UserHB) query.uniqueResult();

    if (userHB != null) {
        // Validate credentials with the token
        String signature = TokenUtils.makeTokenSignature(tokenExpiration, userHB.getUser(),
                userHB.getPassword());

        if (!signature.equals(tokenSignature)) {
            return null;
        }
    }

    return userHB;
}

From source file:com.abiquo.abiserver.persistence.dao.infrastructure.hibernate.DataCenterDAOHibernate.java

License:Open Source License

@Override
public Long getNumberVirtualDatacentersByDatacenter(final Integer idDatacenter) {

    Long numberOfVirtualDatacenters;

    final Session session = HibernateDAOFactory.getSessionFactory().getCurrentSession();
    final Query pmQuery = session.getNamedQuery(DATACENTER_GET_NUMBER_OF_VIRTUAL_DATACENTERS);
    pmQuery.setInteger("idDatacenter", idDatacenter);
    numberOfVirtualDatacenters = (Long) pmQuery.uniqueResult();

    return numberOfVirtualDatacenters;
}

From source file:com.abiquo.abiserver.persistence.dao.infrastructure.hibernate.DataCenterDAOHibernate.java

License:Open Source License

@Override
public DatacenterHB getDatacenterWhereThePrivateNetworkStays(final Integer networkId)
        throws PersistenceException {
    DatacenterHB datacenter;/*from   w  ww .jav a2  s  . c  om*/

    try {
        final Session session = HibernateDAOFactory.getSessionFactory().getCurrentSession();
        final Query query = session.getNamedQuery(DATACENTER_GET_BY_PRIVATE_NETWORK_ID);
        query.setInteger("networkId", networkId);
        datacenter = (DatacenterHB) query.uniqueResult();
    } catch (final HibernateException he) {
        throw new PersistenceException(he.getMessage(), he);
    }

    return datacenter;
}

From source file:com.abiquo.abiserver.persistence.dao.infrastructure.hibernate.DataCenterDAOHibernate.java

License:Open Source License

@Override
public DatacenterHB getDatacenterWhereThePublicNetworkStays(final Integer idNetwork)
        throws PersistenceException {
    DatacenterHB datacenter;// www. j  a v  a  2s  .c  om

    try {
        final Session session = HibernateDAOFactory.getSessionFactory().getCurrentSession();
        final Query query = session.getNamedQuery(DATACENTER_GET_BY_PUBLIC_NETWORK_ID);
        query.setInteger("networkId", idNetwork);
        datacenter = (DatacenterHB) query.uniqueResult();
    } catch (final HibernateException he) {
        throw new PersistenceException(he.getMessage(), he);
    }

    return datacenter;
}

From source file:com.abiquo.abiserver.persistence.dao.infrastructure.hibernate.DataCenterDAOHibernate.java

License:Open Source License

@Override
public DatacenterHB findByName(final String name) throws PersistenceException {
    DatacenterHB datacenter;//from w w w .j av a 2s. c  o  m

    try {
        final Session session = HibernateDAOFactory.getSessionFactory().getCurrentSession();
        final Query query = session.getNamedQuery(GET_DATACENTER_BY_NAME);
        query.setString("name", name);
        datacenter = (DatacenterHB) query.uniqueResult();
    } catch (final NonUniqueResultException e) {
        // No datacenter found
        return null;
    } catch (final HibernateException he) {
        throw new PersistenceException(he.getMessage(), he);
    }

    return datacenter;
}

From source file:com.abiquo.abiserver.persistence.dao.infrastructure.hibernate.HyperVisorDAOHibernate.java

License:Open Source License

@Override
public HypervisorHB getHypervisorFromPhysicalMachine(Integer pmId) {
    Session session = HibernateDAOFactory.getSessionFactory().getCurrentSession();

    Query query = session.getNamedQuery(GET_HYPER_FROM_PHYSICALMACHINE);
    query.setInteger("pmId", pmId);

    return (HypervisorHB) query.uniqueResult();
}

From source file:com.abiquo.abiserver.persistence.dao.infrastructure.hibernate.PhysicalMachineDAOHibernate.java

License:Open Source License

@Override
public Long getNumberOfDeployedVirtualMachines(final PhysicalmachineHB pmHB) {
    Long numberOfDeployedMachines;

    Session session = HibernateDAOFactory.getSessionFactory().getCurrentSession();
    Query pmQuery = session.getNamedQuery(PHYSICALMACHINE_GET_NUMBER_OF_DEPLOYED_MACHINES);
    pmQuery.setInteger("idphysicalmachine", pmHB.getIdPhysicalMachine());
    numberOfDeployedMachines = (Long) pmQuery.uniqueResult();

    return numberOfDeployedMachines;
}

From source file:com.abiquo.abiserver.persistence.dao.infrastructure.hibernate.PhysicalMachineDAOHibernate.java

License:Open Source License

@Override
public String getHypervisorIP(final int machineId) {
    Session session = HibernateDAOFactory.getSessionFactory().getCurrentSession();

    Query pmQuery = session.getNamedQuery(PHYSICALMACHINE_GET_HYPERVISOR_IP);
    pmQuery.setInteger("idphysicalmachine", machineId);

    String hyperIp = (String) pmQuery.uniqueResult();

    return hyperIp;
}