Example usage for javax.persistence PersistenceException PersistenceException

List of usage examples for javax.persistence PersistenceException PersistenceException

Introduction

In this page you can find the example usage for javax.persistence PersistenceException PersistenceException.

Prototype

public PersistenceException(Throwable cause) 

Source Link

Document

Constructs a new PersistenceException exception with the specified cause.

Usage

From source file:icom.jpa.bdk.dao.LabelApplicationDAO.java

public LabelApplication loadObject(ManagedIdentifiableProxy obj, Projection proj) {
    try {//from www  . j ava  2s. co m
        BdkUserContextImpl userContext = (BdkUserContextImpl) obj.getPersistenceContext().getUserContext();
        BeeId id = getBeeId(obj.getObjectId().toString());
        String collabId = id.getId();
        String resourceType = id.getResourceType();
        GetMethod method = prepareGetMethod(resourceType, collabId, proj);
        LabelApplication bdkLabelApplication = (LabelApplication) bdkHttpUtil.execute(LabelApplication.class,
                method, userContext.httpClient);
        return bdkLabelApplication;
    } catch (Exception ex) {
        throw new PersistenceException(ex);
    }
}

From source file:icom.jpa.bdk.dao.CategoryApplicationDAO.java

public CategoryApplication loadObject(ManagedIdentifiableProxy obj, Projection proj) {
    try {/* w  w w  . jav  a2 s  . c om*/
        BdkUserContextImpl userContext = (BdkUserContextImpl) obj.getPersistenceContext().getUserContext();
        BeeId id = getBeeId(obj.getObjectId().toString());
        String collabId = id.getId();
        String resourceType = id.getResourceType();
        GetMethod method = prepareGetMethod(resourceType, collabId, proj);
        CategoryApplication bdkCategoryApplication = (CategoryApplication) bdkHttpUtil
                .execute(CategoryApplication.class, method, userContext.httpClient);
        return bdkCategoryApplication;
    } catch (Exception ex) {
        throw new PersistenceException(ex);
    }
}

From source file:com.impetus.kundera.ejb.EntityResolver.java

/**
 * helper method to recursively build reachable object list.
 * //w  w w . ja  v  a 2s  .  c o  m
 * @param o
 *            the o
 * @param cascadeType
 *            the cascade type
 * @param entities
 *            the entities
 * @return the all reachable entities
 * @throws PropertyAccessException
 *             the property access exception
 */
private void recursivelyResolveEntities(Object o, CascadeType cascadeType, Map<String, EnhancedEntity> entities)
        throws PropertyAccessException {

    EntityMetadata m = null;
    try {
        m = em.getMetadataManager().getEntityMetadata(o.getClass());
    } catch (Exception e) {
        // Object might already be an enhanced entity
    }

    if (m == null) {
        return;
    }

    String id = PropertyAccessorHelper.getId(o, m);

    // Ensure that @Id is set
    if (null == id || id.trim().isEmpty()) {
        throw new PersistenceException(
                "Missing primary key >> " + m.getEntityClazz().getName() + "#" + m.getIdProperty().getName());
    }

    // Dummy name to check if the object is already processed
    String mapKeyForEntity = m.getEntityClazz().getName() + "_" + id;

    if (entities.containsKey(mapKeyForEntity)) {
        return;
    }

    log.debug("Resolving >> " + mapKeyForEntity);

    // Map to hold property-name=>foreign-entity relations
    Map<String, Set<String>> foreignKeysMap = new HashMap<String, Set<String>>();

    // Save to map
    entities.put(mapKeyForEntity, em.getFactory().getEnhancedEntity(o, id, foreignKeysMap));

    // Iterate over EntityMetata.Relation relations
    for (EntityMetadata.Relation relation : m.getRelations()) {

        // Cascade?
        if (!relation.getCascades().contains(CascadeType.ALL)
                && !relation.getCascades().contains(cascadeType)) {
            continue;
        }

        // Target entity
        Class<?> targetClass = relation.getTargetEntity();
        // Mapped to this property
        Field targetField = relation.getProperty();
        // Is it optional?
        boolean optional = relation.isOptional();

        // Value
        Object value = PropertyAccessorHelper.getObject(o, targetField);

        // if object is not null, then proceed
        if (null != value) {

            if (relation.isUnary()) {
                // Unary relation will have single target object.
                String targetId = PropertyAccessorHelper.getId(value,
                        em.getMetadataManager().getEntityMetadata(targetClass));

                Set<String> foreignKeys = new HashSet<String>();

                foreignKeys.add(targetId);
                // put to map
                foreignKeysMap.put(targetField.getName(), foreignKeys);

                // get all other reachable objects from object "value"
                recursivelyResolveEntities(value, cascadeType, entities);

            }
            if (relation.isCollection()) {
                // Collection relation can have many target objects.

                // Value must map to Collection interface.
                @SuppressWarnings("unchecked")
                Collection collection = (Collection) value;

                Set<String> foreignKeys = new HashSet<String>();

                // Iterate over each Object and get the @Id
                for (Object o_ : collection) {
                    String targetId = PropertyAccessorHelper.getId(o_,
                            em.getMetadataManager().getEntityMetadata(targetClass));

                    foreignKeys.add(targetId);

                    // Get all other reachable objects from "o_"
                    recursivelyResolveEntities(o_, cascadeType, entities);
                }
                foreignKeysMap.put(targetField.getName(), foreignKeys);
            }
        }

        // if the value is null
        else {
            // halt, if this was a non-optional property
            if (!optional) {
                throw new PersistenceException(
                        "Missing " + targetClass.getName() + "." + targetField.getName());
            }
        }
    }
}

From source file:com.impetus.kundera.ejb.EntityManagerImpl.java

/**
 * Immediate load and cache.//from www . j a  va2 s .  co m
 * 
 * @param <E>
 *            the element type
 * @param entityClass
 *            the entity class
 * @param primaryKey
 *            the primary key
 * @return the e
 */
protected <E> E immediateLoadAndCache(Class<E> entityClass, Object primaryKey) {
    try {
        EntityMetadata m = metadataManager.getEntityMetadata(entityClass);
        m.setDBType(this.client.getType());
        E e = dataManager.find(entityClass, m, primaryKey.toString());
        if (e != null) {
            session.store(primaryKey, e, m.isCacheable());
        }
        return e;
    } catch (Exception exp) {
        throw new PersistenceException(exp);
    }
}

From source file:com.impetus.kundera.client.PelopsClient.java

@Override
public final <E> E loadColumns(EntityManagerImpl em, Class<E> clazz, String keyspace, String columnFamily,
        String rowId, EntityMetadata m) throws Exception {

    if (!isOpen()) {
        throw new PersistenceException("PelopsClient is closed.");
    }//w w w . ja v  a2  s .c o m
    Selector selector = Pelops.createSelector(poolName, keyspace);
    List<Column> columns = selector.getColumnsFromRow(rowId, columnFamily,
            Selector.newColumnsPredicateAll(true, 10), ConsistencyLevel.ONE);
    E e;
    if (null == columns || columns.size() == 0) {
        e = null;
    } else {
        e = fromThriftRow(em, clazz, m, this.new ThriftRow(rowId, columnFamily, columns));
    }
    return e;
}

From source file:com.impetus.kundera.client.MongoDBClient.java

@Override
public void delete(String idColumnName, String documentName, String rowId) throws Exception {
    DBCollection dbCollection = mongoDb.getCollection(documentName);

    //Find the DBObject to remove first
    BasicDBObject query = new BasicDBObject();
    query.put(idColumnName, rowId);/*from  w ww .java  2  s  .  c  o m*/

    DBCursor cursor = dbCollection.find(query);
    DBObject documentToRemove = null;

    if (cursor.hasNext()) {
        documentToRemove = cursor.next();
    } else {
        throw new PersistenceException(
                "Can't remove Row# " + rowId + " for " + documentName + " because record doesn't exist.");
    }

    dbCollection.remove(documentToRemove);
}

From source file:com.impetus.kundera.ejb.EntityManagerImpl.java

@Override
public final <E> List<E> find(Class<E> entityClass, Object... primaryKeys) {
    if (closed) {
        throw new PersistenceException("EntityManager already closed.");
    }/*from w w  w.j  av  a2  s . co  m*/
    if (primaryKeys == null) {
        throw new IllegalArgumentException("primaryKey value must not be null.");
    }

    // Validate
    metadataManager.validate(entityClass);

    if (null == primaryKeys || primaryKeys.length == 0) {
        return new ArrayList<E>();
    }

    // TODO: load from cache first

    try {
        String[] ids = Arrays.asList(primaryKeys).toArray(new String[] {});
        EntityMetadata m = metadataManager.getEntityMetadata(entityClass);
        m.setDBType(this.client.getType());
        List<E> entities = dataManager.find(entityClass, m, ids);

        // TODO: cache entities for future lookup
        return entities;
    } catch (Exception e) {
        throw new PersistenceException(e);
    }
}

From source file:com.nortal.petit.orm.StatementSupport.java

/**
 * Updates beans by primary key mapping/*from   ww w.j ava2 s .  co  m*/
 * 
 * @param beans
 *            Beans to update
 */
public <B> void update(Collection<B> beans) {
    if (CollectionUtils.isEmpty(beans)) {
        return;
    }

    UpdateStatement<B> stm = new UpdateStatement<B>(getJdbcTemplate(), getStatementBuilder(),
            (B[]) beans.toArray());
    if (stm.getMapping().id() != null) {
        stm.exec();
    } else {
        throw new PersistenceException("Model " + beans.iterator().next().getClass().getSimpleName()
                + " does not have primary key mapping");
    }
}

From source file:icom.jpa.bdk.dao.PresenceDAO.java

public Presence loadObject(ManagedIdentifiableProxy obj, Projection proj) {
    PersistenceContext context = obj.getPersistenceContext();
    BeeId presenceObjId = getBeeId(obj.getObjectId().toString());
    String collabId = presenceObjId.getId();
    String resourceType = "presence";
    try {/*from  w ww. ja  v  a 2 s.  co m*/
        BdkUserContextImpl userContext = (BdkUserContextImpl) context.getUserContext();
        GetMethod method = prepareGetMethod(resourceType, collabId, proj);
        Presence bdkPresence = (Presence) bdkHttpUtil.execute(Presence.class, method, userContext.httpClient);
        return bdkPresence;
    } catch (Exception ex) {
        throw new PersistenceException(ex);
    }
}

From source file:com.impetus.kundera.metadata.MetadataUtils.java

/**
 * Gets the embedded generic object instance.
 * /*from  w w w .  j  a  v  a 2  s  . co  m*/
 * @param embeddedCollectionField
 *            the embedded collection field
 * @return the embedded generic object instance
 */
public static Object getEmbeddedGenericObjectInstance(Field embeddedCollectionField) {
    Class<?> embeddedClass = PropertyAccessorHelper.getGenericClass(embeddedCollectionField);
    Object embeddedObject = null;
    // must have a default no-argument constructor
    try {
        embeddedClass.getConstructor();
        embeddedObject = embeddedClass.newInstance();
    } catch (NoSuchMethodException nsme) {
        throw new PersistenceException(
                embeddedClass.getName() + " is @Embeddable and must have a default no-argument constructor.");
    } catch (InstantiationException e) {
        throw new PersistenceException(embeddedClass.getName() + " could not be instantiated");
    }

    catch (IllegalAccessException e) {
        throw new PersistenceException(embeddedClass.getName() + " could not be accessed");
    }
    return embeddedObject;
}