Example usage for javax.persistence CascadeType ALL

List of usage examples for javax.persistence CascadeType ALL

Introduction

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

Prototype

CascadeType ALL

To view the source code for javax.persistence CascadeType ALL.

Click Source Link

Document

Cascade all operations

Usage

From source file:com.impetus.kundera.mongodb.MongoDBDataHandler.java

public BasicDBObject getDocumentFromEntity(EntityManagerImpl em, EntityMetadata m, Object entity)
        throws PropertyAccessException {
    List<Column> columns = m.getColumnsAsList();
    BasicDBObject dbObj = new BasicDBObject();

    //Populate columns
    for (Column column : columns) {
        try {//w w w .  j  a  v a2 s .  co m
            extractEntityField(entity, dbObj, column);
        } catch (PropertyAccessException e1) {
            log.error("Can't access property " + column.getField().getName());
        }
    }

    //Populate Relationship fields
    List<Relation> relations = m.getRelations();
    for (Relation relation : relations) {
        // Cascade?
        if (!relation.getCascades().contains(CascadeType.ALL)
                && !relation.getCascades().contains(CascadeType.PERSIST)) {
            continue;
        }

        Class<?> embeddedEntityClass = relation.getTargetEntity(); //Target entity
        Field embeddedEntityField = relation.getProperty(); //Mapped to this property         
        boolean optional = relation.isOptional(); // Is it optional         
        Object embeddedObject = PropertyAccessorHelper.getObject(entity, embeddedEntityField); // Value         

        EntityMetadata relMetadata = em.getMetadataManager().getEntityMetadata(embeddedEntityClass);
        relMetadata.addColumn(relMetadata.getIdColumn().getName(), relMetadata.getIdColumn()); //Add PK column

        if (embeddedObject == null) {
            if (!optional) {
                throw new PersistenceException(
                        "Field " + embeddedEntityField + " is not optional, and hence must be set.");
            }

        } else {
            if (relation.isUnary()) {
                BasicDBObject relDBObj = getDocumentFromEntity(em, relMetadata, embeddedObject);
                dbObj.put(embeddedEntityField.getName(), relDBObj);

            } else if (relation.isCollection()) {
                Collection collection = (Collection) embeddedObject;
                BasicDBObject[] relDBObjects = new BasicDBObject[collection.size()];
                int count = 0;
                for (Object o : collection) {
                    relDBObjects[count] = getDocumentFromEntity(em, relMetadata, o);
                    count++;
                }
                dbObj.put(embeddedEntityField.getName(), relDBObjects);
            }

        }

    }
    return dbObj;
}

From source file:com.hmsinc.epicenter.model.surveillance.Anomaly.java

/**
 * @return the method/*  www.j  a  va2  s .  c  o m*/
 */
@ManyToOne(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
@JoinColumn(name = "ID_SURVEILLANCE_METHOD", unique = false, nullable = false, insertable = true, updatable = true)
@org.hibernate.annotations.ForeignKey(name = "FK_ANOMALY_2")
public SurveillanceMethod getMethod() {
    return method;
}

From source file:gov.nih.nci.protexpress.domain.experiment.Experiment.java

/**
 * Gets the contactPerson./*from   w w  w  .  j  a va2  s. c o  m*/
 *
 * @return the contactPerson.
 */
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "contact_person_id")
@Valid
public ContactPerson getContactPerson() {
    return contactPerson;
}

From source file:org.photovault.imginfo.PhotoInfo.java

@History
@OneToOne(cascade = CascadeType.ALL)
@org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
@PrimaryKeyJoinColumn/*w w w . j a  va 2 s.  co  m*/
public ObjectHistory<PhotoInfo> getHistory() {
    return changeHistory;
}

From source file:uk.nhs.cfh.dsp.snomed.expression.model.impl.AbstractExpressionWithFocusConcepts.java

/**
 * Gets the relationships.//from www  . j a v a 2 s  .co  m
 *
 * @return the relationships
 */
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = SnomedRelationshipImpl.class)
@JoinTable(name = "EFC_RELATIONSHIPS", joinColumns = @JoinColumn(name = "uuid"), inverseJoinColumns = @JoinColumn(name = "relationship_id"))
public Collection<SnomedRelationship> getRelationships() {
    return relationships;
}

From source file:onl.netfishers.netshot.device.DeviceGroup.java

/**
 * Gets the check compliance tasks.//www  .j  a  va 2s .c  o m
 *
 * @return the check compliance tasks
 */
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "deviceGroup")
public List<CheckGroupComplianceTask> getCheckComplianceTasks() {
    return checkComplianceTasks;
}

From source file:gov.nih.nci.protexpress.domain.protocol.ProtocolApplication.java

/**
 * Gets the outputs.//from  www .  j  a  v a 2  s .  c  om
 *
 * @return the outputs.
 */
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "protapp_outputs", joinColumns = { @JoinColumn(name = "protapp_id") }, inverseJoinColumns = {
        @JoinColumn(name = "output_id") })
public List<InputOutputObject> getOutputs() {
    return outputs;
}

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

/**
 * helper method to recursively build reachable object list.
 * /*from  ww  w.j a v  a2  s .  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:gov.nih.nci.caarray.domain.file.CaArrayFile.java

/**
 * @return the children//ww  w .j  a  v  a2s.  co m
 */
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
@Cascade({ org.hibernate.annotations.CascadeType.ALL })
public Set<CaArrayFile> getChildren() {
    return children;
}

From source file:uk.nhs.cfh.dsp.srth.information.model.impl.om.ehr.EHRImpl.java

/**
 * Gets the medications.// w w w .  j  a  va 2s  . co  m
 *
 * @return the medications
 */
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = MedicationEventEntry.class)
@JoinTable(name = "EHR_MEDICATIONS", joinColumns = @JoinColumn(name = PATIENT_ID), inverseJoinColumns = @JoinColumn(name = "medication_id"))
@ForeignKey(name = "FK_EHR_MED", inverseName = "FK_MED_EHR")
public Set<BoundClinicalEventEntry> getMedications() {
    return medications;
}