Example usage for javax.persistence CascadeType PERSIST

List of usage examples for javax.persistence CascadeType PERSIST

Introduction

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

Prototype

CascadeType PERSIST

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

Click Source Link

Document

Cascade persist operation

Usage

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

/**
 * @return the organization/*from  w  w  w. ja va2  s  .c  o m*/
 */
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
@JoinColumn(name = "ID_ORGANIZATION", unique = false, nullable = false, insertable = true, updatable = true)
@ForeignKey(name = "FK_SURVEILLANCE_TASK_2")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public Organization getOrganization() {
    return organization;
}

From source file:com.hmsinc.epicenter.model.provider.DataConnection.java

@ManyToMany(cascade = { CascadeType.PERSIST,
        CascadeType.MERGE }, fetch = FetchType.LAZY, targetEntity = Facility.class)
@JoinTable(name = "DATA_CONN_FACILITY", joinColumns = {
        @JoinColumn(name = "ID_DATA_CONNECTION") }, inverseJoinColumns = { @JoinColumn(name = "ID_FACILITY") })
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public Set<Facility> getFacilities() {
    return this.facilities;
}

From source file:gov.nih.nci.firebird.data.OrganizationAssociation.java

/**
 * @return the organization role//from w ww . j  a v a2s . c  om
 */
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, optional = false)
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
@ForeignKey(name = "organization_association_organization_role_fkey")
@JoinColumn(name = "organization_role_id")
public AbstractOrganizationRole getOrganizationRole() {
    return organizationRole;
}

From source file:com.hmsinc.epicenter.model.workflow.Investigation.java

/**
 * @return the createdBy//from   ww w.  j av  a2  s . c om
 */
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST }, fetch = FetchType.LAZY)
@JoinColumn(name = "ID_APP_USER_CREATED_BY", unique = false, nullable = true, insertable = true, updatable = true)
@org.hibernate.annotations.ForeignKey(name = "FK_INVESTIGATION_4")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public EpiCenterUser getCreatedBy() {
    return createdBy;
}

From source file:gov.nih.nci.firebird.data.TrainingCertificate.java

@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH })
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
@JoinColumn(name = "file_id")
@ForeignKey(name = "certificate_firebird_file_fkey")
@NotNull/*  ww  w.  j a  v  a2s  . co  m*/
@Override
public FirebirdFile getFile() {
    return file;
}

From source file:com.hmsinc.epicenter.model.permission.Organization.java

/**
 * @return the authoritativeRegion//from   w  w  w.  j ava2  s  .c o m
 */
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
@JoinColumn(name = "ID_GEOGRAPHY", unique = false, nullable = true, insertable = true, updatable = true)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@ForeignKey(name = "FK_ORGANIZATION_2")
public Geography getAuthoritativeRegion() {
    return authoritativeRegion;
}

From source file:com.hmsinc.epicenter.model.workflow.Investigation.java

/**
 * @return the organization/*  ww w . j  a v a2s  .c  o m*/
 */
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST }, fetch = FetchType.LAZY)
@JoinColumn(name = "ID_ORGANIZATION", unique = false, nullable = false, insertable = true, updatable = true)
@org.hibernate.annotations.ForeignKey(name = "FK_INVESTIGATION_3")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public Organization getOrganization() {
    return organization;
}

From source file:com.eryansky.entity.base.User.java

@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
@JoinTable(name = "T_BASE_USER_ORGAN", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = {
        @JoinColumn(name = "ORGAN_ID") })
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = CacheConstants.HIBERNATE_CACHE_BASE)
public List<Organ> getOrgans() {
    return organs;
}

From source file:com.hmsinc.epicenter.model.permission.Organization.java

/**
 * @return the users//from w  w w.ja  va  2 s.  co m
 */
@ManyToMany(cascade = { CascadeType.PERSIST,
        CascadeType.MERGE }, fetch = FetchType.LAZY, targetEntity = EpiCenterUser.class)
@JoinTable(name = "ORGANIZATION_USER", joinColumns = {
        @JoinColumn(name = "ID_ORGANIZATION") }, inverseJoinColumns = { @JoinColumn(name = "ID_APP_USER") })
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public Set<EpiCenterUser> getUsers() {
    return users;
}

From source file:org.grails.datastore.mapping.mongo.gridfs.GridfsEntityPersister.java

public GridFSInputFile getNative(Object obj, GridFS gridFS, byte[] data) throws IOException {
    Assert.notNull(obj);//from  w  ww  .  j a v  a2 s  .  c  o m
    Assert.notNull(gridFS);

    GridFSInputFile nativeEntry = data == null ? gridFS.createFile() : gridFS.createFile(data);

    // adapted code from NativeEntryEntityPersister.persistEntity()
    SessionImplementor<Object> si = (SessionImplementor<Object>) session;

    ProxyFactory proxyFactory = getProxyFactory();
    // if called internally, obj can potentially be a proxy, which won't work.
    obj = proxyFactory.unwrap(obj);

    PersistentEntity persistentEntity = getMappingContext().getPersistentEntity(obj.getClass().getName());

    final List<PersistentProperty> props = persistentEntity.getPersistentProperties();
    final Map<Association, List<Serializable>> toManyKeys = new HashMap<Association, List<Serializable>>();

    final EntityAccess entityAccess = createEntityAccess(persistentEntity, obj, nativeEntry);
    Object k = readObjectIdentifier(entityAccess, persistentEntity.getMapping());

    for (PersistentProperty prop : props) {
        PropertyMapping<Property> pm = prop.getMapping();
        final Property mappedProperty = pm.getMappedForm();
        boolean ordinalEnum = false;
        String key = null;
        if (mappedProperty != null) {
            key = mappedProperty.getTargetName();
            if (prop.getType().isEnum() && mappedProperty.getEnumTypeObject() == EnumType.ORDINAL) {
                ordinalEnum = true;
            }
        }
        if (key == null)
            key = prop.getName();
        if ((prop instanceof Simple)) {
            Object propValue = entityAccess.getProperty(prop.getName());

            if (ordinalEnum && (propValue instanceof Enum)) {
                propValue = ((Enum) propValue).ordinal();
            }
            setEntryValue(nativeEntry, key, propValue);
        } else if ((prop instanceof Basic)) {
            Basic basic = (Basic) prop;
            CustomTypeMarshaller customTypeMarshaller = basic.getCustomTypeMarshaller();
            if (customTypeMarshaller != null && customTypeMarshaller.supports(getSession().getDatastore())) {
                Object propValue = entityAccess.getProperty(prop.getName());
                customTypeMarshaller.write(prop, propValue, nativeEntry);
            } else {
                Object propValue = entityAccess.getProperty(prop.getName());

                if (!(key.equals("contentType") && propValue == null)
                        && !(key.equals("uploadDate") && propValue == null)
                        && !(key.equals("filename") && propValue == null)
                        && !(key.equals("md5") && propValue == null)) {
                    setEntryValue(nativeEntry, key, propValue);
                }
            }
        } else if ((prop instanceof Custom)) {
            CustomTypeMarshaller customTypeMarshaller = ((Custom) prop).getCustomTypeMarshaller();
            if (customTypeMarshaller.supports(getSession().getDatastore())) {
                Object propValue = entityAccess.getProperty(prop.getName());
                customTypeMarshaller.write(prop, propValue, nativeEntry);
            }
        } else if (prop instanceof OneToMany) {
            final OneToMany oneToMany = (OneToMany) prop;

            final Object propValue = entityAccess.getProperty(oneToMany.getName());
            if (propValue instanceof Collection) {
                Collection associatedObjects = (Collection) propValue;
                if (isInitializedCollection(associatedObjects)) {
                    PersistentEntity associatedEntity = oneToMany.getAssociatedEntity();
                    if (associatedEntity != null) {
                        EntityPersister associationPersister = (EntityPersister) getSession()
                                .getPersister(associatedEntity);
                        if (associationPersister != null) {
                            PersistentCollection persistentCollection;
                            boolean newCollection = false;
                            if (associatedObjects instanceof PersistentCollection) {
                                persistentCollection = (PersistentCollection) associatedObjects;
                            } else {
                                Class associationType = associatedEntity.getJavaClass();
                                persistentCollection = getPersistentCollection(associatedObjects,
                                        associationType);
                                entityAccess.setProperty(oneToMany.getName(), persistentCollection);
                                persistentCollection.markDirty();
                                newCollection = true;
                            }
                            if (persistentCollection.isDirty()) {
                                persistentCollection.resetDirty();
                                List<Serializable> keys = associationPersister.persist(associatedObjects);
                                toManyKeys.put(oneToMany, keys);
                                if (newCollection) {
                                    entityAccess.setProperty(oneToMany.getName(), associatedObjects);
                                }
                            }
                        }
                    }
                }
            }
        } else if (prop instanceof ManyToMany) {
            final ManyToMany manyToMany = (ManyToMany) prop;

            final Object propValue = entityAccess.getProperty(manyToMany.getName());
            if (propValue instanceof Collection) {
                Collection associatedObjects = (Collection) propValue;
                if (isInitializedCollection(associatedObjects)) {
                    setManyToMany(persistentEntity, obj, nativeEntry, manyToMany, associatedObjects,
                            toManyKeys);
                }
            }
        } else if (prop instanceof ToOne) {
            ToOne association = (ToOne) prop;
            if (prop instanceof Embedded) {
                // For embedded properties simply set the entry value, the underlying implementation
                // will have to store the embedded entity in an appropriate way (as a sub-document in a document store for example)
                handleEmbeddedToOne(association, key, entityAccess, nativeEntry);
            } else if (association.doesCascade(CascadeType.PERSIST)
                    && association.getAssociatedEntity() != null) {
                final Object associatedObject = entityAccess.getProperty(prop.getName());
                if (associatedObject != null) {
                    Serializable associationId;
                    NativeEntryEntityPersister associationPersister = (NativeEntryEntityPersister) getSession()
                            .getPersister(associatedObject);
                    if (proxyFactory.isInitialized(associatedObject)
                            && !getSession().contains(associatedObject)) {
                        Serializable tempId = associationPersister.getObjectIdentifier(associatedObject);
                        if (tempId == null) {
                            if (association.isOwningSide()) {
                                tempId = getSession().persist(associatedObject);
                            }
                        }
                        associationId = tempId;
                    } else {
                        associationId = associationPersister.getObjectIdentifier(associatedObject);
                    }

                    // handling of hasOne inverse key
                    if (association.isForeignKeyInChild()) {
                        DBObject cachedAssociationEntry = (DBObject) si
                                .getCachedEntry(association.getAssociatedEntity(), associationId);
                        if (cachedAssociationEntry != null) {
                            if (association.isBidirectional()) {
                                Association inverseSide = association.getInverseSide();
                                if (inverseSide != null) {
                                    setEntryValue(cachedAssociationEntry, inverseSide.getName(),
                                            formulateDatabaseReference(association.getAssociatedEntity(),
                                                    inverseSide, (Serializable) k));
                                } else {
                                    setEntryValue(cachedAssociationEntry, key, formulateDatabaseReference(
                                            association.getAssociatedEntity(), inverseSide, (Serializable) k));
                                }
                            }
                        }

                        if (association.doesCascade(CascadeType.PERSIST)) {

                            if (association.isBidirectional()) {
                                Association inverseSide = association.getInverseSide();
                                if (inverseSide != null) {
                                    EntityAccess inverseAccess = new EntityAccess(inverseSide.getOwner(),
                                            associatedObject);
                                    inverseAccess.setProperty(inverseSide.getName(), obj);
                                }
                            }
                            associationPersister.persist(associatedObject);
                        }
                    }
                    // handle of standard many-to-one
                    else {
                        if (associationId != null) {
                            setEntryValue(nativeEntry, key,
                                    formulateDatabaseReference(persistentEntity, association, associationId));

                            if (association.isBidirectional()) {
                                Association inverse = association.getInverseSide();
                                // unwrap the entity in case it is a proxy, since we may need to update the reverse link.
                                Object inverseEntity = proxyFactory
                                        .unwrap(entityAccess.getProperty(association.getName()));
                                if (inverseEntity != null) {
                                    EntityAccess inverseAccess = createEntityAccess(
                                            association.getAssociatedEntity(), inverseEntity);
                                    Object entity = entityAccess.getEntity();
                                    if (inverse instanceof OneToMany) {
                                        Collection existingValues = (Collection) inverseAccess
                                                .getProperty(inverse.getName());
                                        if (existingValues == null) {
                                            existingValues = MappingUtils
                                                    .createConcreteCollection(inverse.getType());
                                            inverseAccess.setProperty(inverse.getName(), existingValues);
                                        }
                                        if (!existingValues.contains(entity))
                                            existingValues.add(entity);
                                    } else if (inverse instanceof ToOne) {
                                        inverseAccess.setProperty(inverse.getName(), entity);
                                    }
                                }
                            }
                        }
                    }
                } else {
                    setEntryValue(nativeEntry, getPropertyKey(prop), null);
                }
            }
        } else if (prop instanceof EmbeddedCollection) {
            handleEmbeddedToMany(entityAccess, nativeEntry, prop, key);
        }
    }

    return nativeEntry;
}