Example usage for org.hibernate Session load

List of usage examples for org.hibernate Session load

Introduction

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

Prototype

void load(Object object, Serializable id);

Source Link

Document

Read the persistent state associated with the given identifier into the given transient instance.

Usage

From source file:com.alejandro.modelo.ModeloImagen.java

public static void delete(String id) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();//from  w  w w  .jav a 2s .  co m

    Imagen p = (Imagen) session.load(Imagen.class, Integer.parseInt(id));
    session.delete(p);

    session.getTransaction().commit();
    session.flush();
    session.close();
}

From source file:com.alejandro.modelo.ModeloInmueble.java

public static void delete(String id) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();//from  w  ww . jav a2  s .c o  m

    Inmueble p = (Inmueble) session.load(Inmueble.class, Integer.parseInt(id));
    session.delete(p);

    session.getTransaction().commit();
    session.flush();
    session.close();
}

From source file:com.amalto.core.storage.hibernate.FlatTypeMapping.java

License:Open Source License

private Object createReferencedObject(Session session, ComplexTypeMetadata referencedType,
        Class<?> referencedClass, Object referencedIdValue)
        throws InstantiationException, IllegalAccessException {
    if (referencedIdValue == null) {
        return null; // Means no reference (reference is null).
    }/*ww w .j  a v  a2s. c  o  m*/
    if (referencedIdValue instanceof Wrapper) {
        return referencedIdValue; // It's already the referenced object.
    }
    // Try to load object from current session
    if (referencedIdValue instanceof DataRecord) {
        Serializable referencedValueId;
        DataRecord idAsDataRecord = (DataRecord) referencedIdValue;
        Collection<FieldMetadata> keyFields = idAsDataRecord.getType().getKeyFields();
        if (keyFields.size() == 1) {
            referencedValueId = (Serializable) idAsDataRecord.get(keyFields.iterator().next());
        } else {
            ClassLoader storageClassLoader = Thread.currentThread().getContextClassLoader();
            List<Object> ids = new LinkedList<Object>();
            for (FieldMetadata keyField : keyFields) {
                ids.add(idAsDataRecord.get(keyField));
            }
            referencedValueId = createCompositeId(storageClassLoader, referencedClass, ids);
        }
        // should actually load the object to validate the FK (for Record Validation), check by ThreadLocal won't affect performance or block function
        if (DataRecord.ValidateRecord.get()) {
            Object sessionObject = session.get(referencedClass, referencedValueId);
            if (sessionObject == null) {
                throw new ValidateException(
                        "Invalid foreign key: [" + referencedClass.getName() + "#" + referencedValueId //$NON-NLS-1$ //$NON-NLS-2$
                                + "] doesn't exist."); //$NON-NLS-1$
            } else {
                return sessionObject;
            }
        } else {
            Object sessionObject = session.load(referencedClass, referencedValueId);
            if (sessionObject != null) {
                return sessionObject;
            }
        }
    } else {
        throw new NotImplementedException("Unexpected state."); //$NON-NLS-1$
    }
    Class<?> fieldJavaType = referencedIdValue.getClass();
    // Null package might happen with proxy classes generated by Hibernate
    if (fieldJavaType.getPackage() != null && fieldJavaType.getPackage().getName().startsWith("java.")) { //$NON-NLS-1$
        Wrapper referencedObject = (Wrapper) referencedClass.newInstance();
        for (FieldMetadata fieldMetadata : referencedType.getFields()) {
            if (fieldMetadata.isKey()) {
                referencedObject.set(fieldMetadata.getName(), referencedIdValue);
            }
        }
        return referencedObject;
    } else {
        return referencedIdValue;
    }
}

From source file:com.amalto.core.storage.hibernate.ScatteredTypeMapping.java

License:Open Source License

private Object getReferencedObject(ClassLoader storageClassLoader, Session session,
        ComplexTypeMetadata referencedType, Object referencedIdValue) {
    Class<?> referencedClass;
    try {/*from ww  w . ja  v a  2s.c  om*/
        referencedClass = ((StorageClassLoader) storageClassLoader).getClassFromType(referencedType);
    } catch (Exception e) {
        throw new RuntimeException("Could not get class for type '" + referencedType.getName() + "'", e);
    }
    try {
        if (referencedIdValue == null) {
            return null; // Means no reference (reference is null).
        }
        if (referencedIdValue instanceof Wrapper) {
            return referencedIdValue; // It's already the referenced object.
        }
        // Try to load object from current session
        if (referencedIdValue instanceof List) {
            // Handle composite id values
            Serializable result;
            try {
                Class<?> idClass = storageClassLoader.loadClass(referencedClass.getName() + "_ID"); //$NON-NLS-1$
                Class[] parameterClasses = new Class[((List) referencedIdValue).size()];
                int i = 0;
                for (Object o : (List) referencedIdValue) {
                    if (o == null) {
                        throw new IllegalStateException("Id cannot have a null value.");
                    }
                    parameterClasses[i++] = o.getClass();
                }
                Constructor<?> constructor = idClass.getConstructor(parameterClasses);
                result = (Serializable) constructor.newInstance(((List) referencedIdValue).toArray());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            Serializable referencedValueId = result;
            // should actually load the object to validate the FK (for Record Validation), check by ThreadLocal won't affect performance or block function
            if (DataRecord.ValidateRecord.get()) {
                Object sessionObject = session.get(referencedClass, referencedValueId);
                if (sessionObject == null) {
                    throw new ValidateException(
                            "Invalid foreign key: [" + referencedClass.getName() + "#" + referencedValueId //$NON-NLS-1$ //$NON-NLS-2$
                                    + "] doesn't exist."); //$NON-NLS-1$
                } else {
                    return sessionObject;
                }
            } else {
                Object sessionObject = session.load(referencedClass, referencedValueId);
                if (sessionObject != null) {
                    return sessionObject;
                }
            }
        } else if (referencedIdValue instanceof Serializable) {
            // should actually load the object to validate the FK (for Record Validation), check by ThreadLocal won't affect performance or block function
            if (DataRecord.ValidateRecord.get()) {
                Object sessionObject = session.get(referencedClass, (Serializable) referencedIdValue);
                if (sessionObject == null) {
                    throw new ValidateException("Invalid foreign key: [" + referencedClass.getName() + "#" //$NON-NLS-1$//$NON-NLS-2$
                            + (Serializable) referencedIdValue + "] doesn't exist."); //$NON-NLS-1$
                } else {
                    return sessionObject;
                }
            } else {
                Object sessionObject = session.load(referencedClass, (Serializable) referencedIdValue);
                if (sessionObject != null) {
                    return sessionObject;
                }
            }
        } else {
            throw new NotImplementedException("Unexpected state.");
        }
        Class<?> fieldJavaType = referencedIdValue.getClass();
        // Null package might happen with proxy classes generated by Hibernate
        if (fieldJavaType.getPackage() != null && fieldJavaType.getPackage().getName().startsWith("java.")) { //$NON-NLS-1$
            Wrapper referencedObject = (Wrapper) referencedClass.newInstance();
            for (FieldMetadata fieldMetadata : referencedType.getFields()) {
                if (fieldMetadata.isKey()) {
                    referencedObject.set(fieldMetadata.getName(), referencedIdValue);
                }
            }
            return referencedObject;
        } else {
            return referencedIdValue;
        }
    } catch (Exception e) {
        throw new RuntimeException("Could not create referenced object of type '" + referencedClass
                + "' with id '" + String.valueOf(referencedIdValue) + "'", e);
    }
}

From source file:com.amalto.core.storage.hibernate.SystemScatteredTypeMapping.java

License:Open Source License

private Object getReferencedObject(ClassLoader storageClassLoader, Session session,
        ComplexTypeMetadata referencedType, Object referencedIdValue) {
    Class<?> referencedClass;
    try {/*from w  ww .ja v a  2s  .c o m*/
        referencedClass = ((StorageClassLoader) storageClassLoader).getClassFromType(referencedType);
    } catch (Exception e) {
        throw new RuntimeException("Could not get class for type '" + referencedType.getName() + "'", e);
    }
    try {
        if (referencedIdValue == null) {
            return null; // Means no reference (reference is null).
        }
        if (referencedIdValue instanceof Wrapper) {
            return referencedIdValue; // It's already the referenced object.
        }
        // Try to load object from current session
        if (referencedIdValue instanceof List) {
            // Handle composite id values
            Serializable result;
            try {
                Class<?> idClass = storageClassLoader.loadClass(referencedClass.getName() + "_ID"); //$NON-NLS-1$
                Class[] parameterClasses = new Class[((List) referencedIdValue).size()];
                int i = 0;
                for (Object o : (List) referencedIdValue) {
                    if (o == null) {
                        throw new IllegalStateException("Id cannot have a null value.");
                    }
                    parameterClasses[i++] = o.getClass();
                }
                Constructor<?> constructor = idClass.getConstructor(parameterClasses);
                result = (Serializable) constructor.newInstance(((List) referencedIdValue).toArray());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            Serializable referencedValueId = result;

            Object sessionObject = session.load(referencedClass, referencedValueId);
            if (sessionObject != null) {
                return sessionObject;
            }
        } else if (referencedIdValue instanceof Serializable) {
            Object sessionObject = session.load(referencedClass, (Serializable) referencedIdValue);
            if (sessionObject != null) {
                return sessionObject;
            }
        } else {
            throw new NotImplementedException("Unexpected state.");
        }
        Class<?> fieldJavaType = referencedIdValue.getClass();
        // Null package might happen with proxy classes generated by Hibernate
        if (fieldJavaType.getPackage() != null && fieldJavaType.getPackage().getName().startsWith("java.")) { //$NON-NLS-1$
            Wrapper referencedObject = (Wrapper) referencedClass.newInstance();
            for (FieldMetadata fieldMetadata : referencedType.getFields()) {
                if (fieldMetadata.isKey()) {
                    referencedObject.set(fieldMetadata.getName(), referencedIdValue);
                }
            }
            return referencedObject;
        } else {
            return referencedIdValue;
        }
    } catch (Exception e) {
        throw new RuntimeException("Could not create referenced object of type '" + referencedClass
                + "' with id '" + String.valueOf(referencedIdValue) + "'", e);
    }
}

From source file:com.anite.zebra.hivemind.impl.ClusterSafeLockManager.java

License:Apache License

/**
 * @param processInstance//www.j  a v a2s.  c o  m
 * @throws LockException
 */
public void releaseLock(IProcessInstance processInstance, Session session) throws LockException {

    try {
        DatabaseLock lock = (DatabaseLock) session.load(DatabaseLock.class,
                processInstance.getProcessInstanceId());
        Transaction t = session.beginTransaction();
        session.delete(lock);
        t.commit();

    } catch (HibernateException e) {
        log.error("Releasing Lock should never fail ", e);
        throw new LockException(e);
    }
}

From source file:com.anite.zebra.hivemind.impl.PriorityManager.java

License:Apache License

/**
 * Fetch the default priority/*  w w w  .j  a  v a  2  s  .  c o  m*/
 * 
 * @return
 */
public Priority getDefaultPriority() {

    Session session = RegistryHelper.getInstance().getSession();

    return (Priority) session.load(Priority.class, this.defaultPriorityId);

}

From source file:com.appeligo.search.actions.admin.AdminAction.java

License:Apache License

public String validateUser() {
    if (u > 0) {
        Session session = HibernateUtil.currentSession();
        User user = (User) session.load(User.class, u);
        user.setRegistrationComplete(true);
        users = User.getUsers();/*from  w w w  .j a va  2s.c  o m*/
        return SUCCESS;
    } else {
        addActionError("Unable to indentify the user to validate.");
        users = User.getUsers();
        return ERROR;
    }
}

From source file:com.appeligo.search.actions.admin.AdminAction.java

License:Apache License

public String validateSMS() {
    if (u > 0) {
        Session session = HibernateUtil.currentSession();
        User user = (User) session.load(User.class, u);
        user.setSmsVerified(true);/*from w  ww .  j a  v  a  2 s  .co m*/
        users = User.getUsers();
        return SUCCESS;
    } else {
        addActionError("Unable to indentify the user for this mobile device.");
        users = User.getUsers();
        return ERROR;
    }

}

From source file:com.appeligo.search.actions.admin.AdminAction.java

License:Apache License

public String enableUser() {
    if (u > 0) {
        Session session = HibernateUtil.currentSession();
        User user = (User) session.load(User.class, u);
        user.setEnabled(!user.isEnabled());
        users = User.getUsers();//w  ww.  jav a  2s  .co m
        return SUCCESS;
    } else {
        addActionError("Unable to indentify the user to enable/disable");
        users = User.getUsers();
        return ERROR;
    }

}