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:com.impetus.kundera.db.accessor.DocumentDataAccessor.java

@Override
public void write(EnhancedEntity e, EntityMetadata m) throws Exception {
    String entityName = e.getEntity().getClass().getName();
    String id = e.getId();/* w w  w .ja v a 2  s  .  co m*/

    log.debug("Document >> Write >> " + entityName + "_" + id);
    if (m.getIdColumn() == null) {
        throw new PersistenceException("Primary key must be annotated with @Column");
    }

    m.addColumn(m.getIdColumn().getName(), m.getIdColumn()); //Add PK column      
    getEntityManager().getClient().writeColumns(getEntityManager(), e, m);

}

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

/**
 * Checks the validity of a class for Cassandra entity.
 * //from ww  w  .j  a  va 2s.  co m
 * @param clazz
 *            validates this class
 * 
 * @return returns 'true' if valid
 */
@Override
// TODO: reduce Cyclomatic complexity
public final void validate(final Class<?> clazz) {

    if (classes.contains(clazz)) {
        return;
    }

    LOG.debug("Validating " + clazz.getName());

    // Is Entity?
    if (!clazz.isAnnotationPresent(Entity.class)) {
        throw new PersistenceException(clazz.getName() + " is not annotated with @Entity");
    }

    // must have a default no-argument constructor
    try {
        clazz.getConstructor();
    } catch (NoSuchMethodException nsme) {
        throw new PersistenceException(clazz.getName() + " must have a default no-argument constructor.");
    }

    // what type is it? ColumnFamily or SuperColumnFamily, Document or simply relational entity?
    if (clazz.isAnnotationPresent(SuperColumnFamily.class) || clazz.isAnnotationPresent(ColumnFamily.class)) {
        LOG.debug("Entity is for NoSQL database: " + clazz.getName());
    } else if (!clazz.isAnnotationPresent(Document.class)) {
        LOG.debug("Entity is for document based database: " + clazz.getName());
    } else {
        LOG.debug("Entity is for relational database table: " + clazz.getName());
    }

    // check for @Key and ensure that there is just 1 @Key field of String
    // type.
    List<Field> keys = new ArrayList<Field>();
    for (Field field : clazz.getDeclaredFields()) {
        if (field.isAnnotationPresent(Id.class)) {
            keys.add(field);
        }
    }

    if (keys.size() == 0) {
        throw new PersistenceException(clazz.getName() + " must have an @Id field.");
    } else if (keys.size() > 1) {
        throw new PersistenceException(clazz.getName() + " can only have 1 @Id field.");
    }

    if (!keys.get(0).getType().equals(String.class)) {
        throw new PersistenceException(clazz.getName() + " @Id must be of String type.");
    }

    // save in cache
    classes.add(clazz);
}

From source file:com.impetus.kundera.ejb.event.EntityEventDispatcher.java

/**
 * Fire event listeners./*from  w w  w .j a v a  2s . co m*/
 * 
 * @param metadata
 *            the metadata
 * @param entity
 *            the entity
 * @param event
 *            the event
 * @throws PersistenceException
 *             the persistence exception
 */
public void fireEventListeners(EntityMetadata metadata, Object entity, Class<?> event)
        throws PersistenceException {

    // handle external listeners first
    List<? extends CallbackMethod> callBackMethods = metadata.getCallbackMethods(event);

    if (null != callBackMethods && !callBackMethods.isEmpty()) {
        log.debug("Callback >> " + event.getSimpleName() + " on " + metadata.getEntityClazz().getName());
        for (CallbackMethod callback : callBackMethods) {
            log.debug("Firing >> " + callback);
            try {
                callback.invoke(entity);
            } catch (Exception e) {
                throw new PersistenceException(e);
            }
        }
    }
}

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

@Override
@Deprecated//  w  w w  .  j  a v  a2 s  .  co  m
public void writeColumns(String dbName, String documentName, String key, List<Column> columns, EnhancedEntity e)
        throws Exception {
    throw new PersistenceException("Not yet implemented");
}

From source file:jef.database.query.function.NoArgSQLFunction.java

@SuppressWarnings("unchecked")
public Expression renderExpression(List<Expression> args) {
    if (args.size() > 0) {
        throw new PersistenceException("function takes no arguments: " + name);
    }/*from w ww. j  a v a  2 s . c o  m*/
    Function func = new Function(name);
    if (hasParenthesesIfNoArguments) {
        func.setParameters(new ExpressionList(Collections.EMPTY_LIST));
    }
    return func;
}

From source file:com.abiquo.server.core.infrastructure.DatastoreDAO.java

/**
 * Use the ''datastoreUUID'' property in order to return all the representations of a shared
 * datastore.//from ww  w  .j a  v  a  2 s  .  co m
 * 
 * @return all the datastores with the same ''datastore uuid'' INCLUDING the provided datastore.
 */
public List<Datastore> findShares(final Datastore datastore) {
    if (datastore.getDatastoreUUID() == null) {
        throw new PersistenceException("Datastore doesn't have set the UUID");
    }

    Criteria crit = createCriteria(sharedUuid(datastore.getDatastoreUUID()));

    return getResultList(crit);
}

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

/**
 * Resolve all reachable entities from entity
 * /* w w  w  . j a  v  a2  s. c om*/
 * @param entity
 *            the entity
 * @param cascadeType
 *            the cascade type
 * @return the all reachable entities
 */
public List<EnhancedEntity> resolve(Object entity, CascadeType cascadeType, DBType dbType) {
    Map<String, EnhancedEntity> map = new HashMap<String, EnhancedEntity>();
    try {
        log.debug("Resolving reachable entities for cascade " + cascadeType);

        //For Document-based data-stores, entities need to be embedded         
        if (dbType.equals(DBType.MONGODB)) {
            resolveEmbeddedEntities(entity, cascadeType, map);
        } else {
            recursivelyResolveEntities(entity, cascadeType, map);
        }

    } catch (PropertyAccessException e) {
        throw new PersistenceException(e.getMessage());
    }

    if (log.isDebugEnabled()) {
        for (Map.Entry<String, EnhancedEntity> entry : map.entrySet()) {
            log.debug(
                    "Entity => " + entry.getKey() + ", ForeignKeys => " + entry.getValue().getForeignKeysMap());
        }
    }

    return new ArrayList<EnhancedEntity>(map.values());
}

From source file:com.abiquo.server.core.infrastructure.RemoteServiceDAO.java

public String getRemoteServiceUri(final Datacenter datacenter, final RemoteServiceType type) {
    List<RemoteService> nodecollectors = findByDatacenterAndType(datacenter, type);

    if (nodecollectors.size() != 1 || nodecollectors.get(0).getStatus() == 0) {
        final String cause = String.format(
                "Datacenter [%s] doesn't have the ''[%s]'' remote service " + "configured",
                datacenter.getName(), type.getName());

        throw new PersistenceException(cause); // TODO infrastructure exception
    }/* ww  w.ja v a  2s .c o m*/

    return nodecollectors.get(0).getUri();
}

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

private void resolveEmbeddedEntities(Object o, CascadeType cascadeType, Map<String, EnhancedEntity> entities)
        throws PropertyAccessException {
    EntityMetadata m = em.getMetadataManager().getEntityMetadata(o.getClass());

    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());
    }//from w  w w . j ava  2  s.  c  o  m

    String mapKeyForEntity = m.getEntityClazz().getName() + "_" + id;
    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));
}

From source file:com.impetus.kundera.metadata.processor.EntityListenersProcessor.java

@Override
public final void process(final Class<?> entityClass, EntityMetadata metadata) {

    // list all external listeners first.
    EntityListeners entityListeners = (EntityListeners) entityClass.getAnnotation(EntityListeners.class);
    if (entityListeners != null) {
        Class<?>[] entityListenerClasses = entityListeners.value();
        if (entityListenerClasses != null) {
            // iterate through all EntityListeners
            for (Class<?> entityListener : entityListenerClasses) {

                // entityListener class must have a no-argument constructor
                try {
                    entityListener.getConstructor();
                } catch (NoSuchMethodException nsme) {
                    throw new PersistenceException("Skipped method(" + entityListener.getName()
                            + ") must have a default no-argument constructor.");
                }//from  w  ww  .ja  v a 2s  .  c o m

                // iterate through all public methods
                for (Method method : entityListener.getDeclaredMethods()) {

                    // find valid jpa annotations for this method
                    List<Class<?>> jpaAnnotations = getValidJPAAnnotationsFromMethod(entityListener, method, 1);

                    // add them all to metadata
                    for (Class<?> jpaAnnotation : jpaAnnotations) {
                        CallbackMethod callBackMethod = metadata.new ExternalCallbackMethod(entityListener,
                                method);
                        addCallBackMethod(metadata, jpaAnnotation, callBackMethod);
                    }
                }
            }
        }
    }

    // list all internal listeners now.
    // iterate through all public methods of entityClass
    // since this is already an @Entity class, it will sure have a default
    // no-arg constructor
    for (Method method : entityClass.getDeclaredMethods()) {
        // find valid jpa annotations for this method
        List<Class<?>> jpaAnnotations = getValidJPAAnnotationsFromMethod(entityClass, method, 0);
        // add them all to metadata
        for (Class<?> jpaAnnotation : jpaAnnotations) {
            CallbackMethod callbackMethod = metadata.new InternalCallbackMethod(method);
            addCallBackMethod(metadata, jpaAnnotation, callbackMethod);
        }
    }
}