Example usage for org.hibernate.metadata ClassMetadata setIdentifier

List of usage examples for org.hibernate.metadata ClassMetadata setIdentifier

Introduction

In this page you can find the example usage for org.hibernate.metadata ClassMetadata setIdentifier.

Prototype

void setIdentifier(Object entity, Serializable id, SharedSessionContractImplementor session);

Source Link

Document

Inject the identifier value into the given entity.

Usage

From source file:com.oracle.coherence.hibernate.cachestore.HibernateCacheLoader.java

License:CDDL license

/**
 * Ensure that there are no conflicts between an explicit and implicit key.
 *
 * @param id     the explicit key/*from   w  ww  .j av a  2 s.c  o  m*/
 * @param entity an entity (containing an implicit key)
 * @param sessionImplementor the Hibernate SessionImplementor
 */
protected void validateIdentifier(Serializable id, Object entity, SessionImplementor sessionImplementor) {
    ClassMetadata classMetaData = getEntityClassMetadata();

    Serializable intrinsicIdentifier = classMetaData.getIdentifier(entity, sessionImplementor);

    if (intrinsicIdentifier == null) {
        classMetaData.setIdentifier(entity, (Serializable) id, sessionImplementor);
    } else {
        if (!intrinsicIdentifier.equals(id)) {
            throw new IllegalArgumentException(
                    "Conflicting identifier " + "information between entity " + entity + " and id " + id);
        }
    }
}

From source file:org.projectforge.database.xstream.HibernateXmlConverter.java

License:Open Source License

/**
 * @param writer/*  www  . jav a2 s.  c  om*/
 * @param includeHistory
 * @param session
 * @throws DataAccessException
 * @throws HibernateException
 */
private void writeObjects(final Writer writer, final boolean includeHistory, final Session session,
        final boolean preserveIds) throws DataAccessException, HibernateException {
    // Container fr die Objekte
    final List<Object> all = new ArrayList<Object>();
    final XStream stream = initXStream(session, true);
    final XStream defaultXStream = initXStream(session, false);

    session.flush();
    // Alles laden
    List<?> list = session.createQuery("select o from java.lang.Object o").setReadOnly(true).list();
    list = (List<?>) CollectionUtils.select(list, PredicateUtils.uniquePredicate());
    final int size = list.size();
    log.info("Writing " + size + " objects");
    for (final Iterator<?> it = list.iterator(); it.hasNext();) {
        final Object obj = it.next();
        if (log.isDebugEnabled()) {
            log.debug("loaded object " + obj);
        }
        if ((obj instanceof HistoryEntry || obj instanceof PropertyDelta) && includeHistory == false) {
            continue;
        }
        Hibernate.initialize(obj);
        Class<?> targetClass = obj.getClass();
        while (Enhancer.isEnhanced(targetClass) == true) {
            targetClass = targetClass.getSuperclass();
        }
        final ClassMetadata classMetadata = session.getSessionFactory().getClassMetadata(targetClass);
        if (classMetadata == null) {
            log.fatal("Can't init " + obj + " of type " + targetClass);
            continue;
        }
        // initalisierung des Objekts...
        defaultXStream.marshal(obj, new CompactWriter(new NullWriter()));

        if (preserveIds == false) {
            // Nun kann die ID gelscht werden
            classMetadata.setIdentifier(obj, null, EntityMode.POJO);
        }
        if (log.isDebugEnabled()) {
            log.debug("loading evicted object " + obj);
        }
        if (this.ignoreFromTopLevelListing.contains(targetClass) == false) {
            all.add(obj);
        }
    }
    // und schreiben
    try {
        writer.write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
    } catch (final IOException ex) {
        // ignore, will fail on stream.marshal()
    }
    log.info("Wrote " + all.size() + " objects");
    final MarshallingStrategy marshallingStrategy = new ProxyIdRefMarshallingStrategy();
    stream.setMarshallingStrategy(marshallingStrategy);
    stream.marshal(all, new PrettyPrintWriter(writer));
}

From source file:org.shept.persistence.provider.DaoUtils.java

License:Apache License

/**
 * This deepCopy will copy all properties of the template model but ignore
 * the id. In case the id is a compound id the the resulting copy will get a
 * deepCopy of the id if the composite id is only partially filled
 * //from  w  w w .ja va2s  .  c o m
 * NOTE that the method will only create shallow copies except for component
 * properties which will be deep copied This means that collections and
 * associations will NOT be deep copied
 * 
 * @param dao
 * @param entityModelTemplate
 * @return a copy of the entityModelTemplate
 */
public static Object deepCopyModel(DaoSupport dao, Object entityModelTemplate) {
    ClassMetadata modelMeta = getClassMetadata(dao, entityModelTemplate);
    if (null == modelMeta) {
        return null;
    }
    Object modelCopy = shallowCopyModel(dao, entityModelTemplate, false);

    // Ids of new models are either null or composite ids and will be copied
    // if new
    boolean isCopyId = isNewModel(dao, entityModelTemplate);
    Object idValue = modelMeta.getIdentifier(entityModelTemplate, EntityMode.POJO);
    if (null != idValue && isCopyId) {
        String idName = modelMeta.getIdentifierPropertyName();
        Object idCopy = BeanUtils.instantiateClass(idValue.getClass());
        BeanUtils.copyProperties(idValue, idCopy, new String[] { idName });
        modelMeta.setIdentifier(modelCopy, (Serializable) idCopy, EntityMode.POJO);
    }

    String[] names = modelMeta.getPropertyNames();
    Type[] types = modelMeta.getPropertyTypes();
    for (int i = 0; i < modelMeta.getPropertyNames().length; i++) {
        if (types[i].isComponentType()) {
            String propName = names[i];
            Object propValue = modelMeta.getPropertyValue(entityModelTemplate, propName, EntityMode.POJO);
            Object propCopy = shallowCopyModel(dao, propValue, true);
            modelMeta.setPropertyValue(modelCopy, propName, propCopy, EntityMode.POJO);
        }
    }
    return modelCopy;
}