List of usage examples for javax.persistence CascadeType PERSIST
CascadeType PERSIST
To view the source code for javax.persistence CascadeType PERSIST.
Click Source Link
From source file:com.hmsinc.epicenter.model.permission.EpiCenterUser.java
/** * @return the organizations/*from ww w .ja v a 2s . co m*/ */ @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "users", targetEntity = Organization.class) @ForeignKey(name = "FK_ORGANIZATION_USER_1", inverseName = "FK_ORGANIZATION_USER_2") @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) public Set<Organization> getOrganizations() { return organizations; }
From source file:com.hmsinc.epicenter.model.permission.EpiCenterUser.java
/** * @return the assignedEvents//from ww w. ja v a2 s. c om */ @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "assignedTo") public Set<Investigation> getAssignedEvents() { return assignedEvents; }
From source file:com.hmsinc.epicenter.model.permission.EpiCenterUser.java
/** * @return the activities/* ww w . ja v a 2 s.co m*/ */ @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "user") public Set<Activity> getActivities() { return activities; }
From source file:org.opennms.netmgt.model.OnmsNode.java
/** * <p>getCategories</p>//w ww. jav a 2s . co m * * @return a {@link java.util.Set} object. */ @XmlElement(name = "categories") @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }) @JoinTable(name = "category_node", joinColumns = { @JoinColumn(name = "nodeId") }, inverseJoinColumns = { @JoinColumn(name = "categoryId") }) public Set<OnmsCategory> getCategories() { return m_categories; }
From source file:edu.harvard.med.screensaver.model.libraries.Library.java
@ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE }) @JoinColumn(name = "ownerScreenerId", nullable = true) @org.hibernate.annotations.ForeignKey(name = "fk_library_to_owner") // @org.hibernate.annotations.LazyToOne(value=org.hibernate.annotations.LazyToOneOption.PROXY) @org.hibernate.annotations.Cascade(value = { org.hibernate.annotations.CascadeType.SAVE_UPDATE }) public ScreeningRoomUser getOwner() { return _owner; }
From source file:edu.ku.brc.specify.tools.datamodelgenerator.DatamodelGenerator.java
/** * @param className/* w w w.jav a 2s .co m*/ * @param tableList */ @SuppressWarnings("cast") protected void processClass(final String className, final List<Table> tableList) { try { Class<?> classObj = Class.forName(packageName + "." + className); Table table = null; String tableName = null; if (classObj.isAnnotationPresent(javax.persistence.Table.class)) { Vector<TableIndex> indexes = new Vector<TableIndex>(); javax.persistence.Table tableAnno = (javax.persistence.Table) classObj .getAnnotation(javax.persistence.Table.class); tableName = tableAnno.name(); org.hibernate.annotations.Table hiberTableAnno = (org.hibernate.annotations.Table) classObj .getAnnotation(org.hibernate.annotations.Table.class); if (hiberTableAnno != null) { //System.out.println("Table Indexes: "); for (Index index : hiberTableAnno.indexes()) { //System.out.println(" "+index.name() + " "+ index.columnNames()); indexes.add(new TableIndex(index.name(), index.columnNames())); } } table = createTable(packageName + "." + className, tableName); if (includeDesc) { table.setDesc(getTableDesc(tableName)); table.setNameDesc(getTableNameDesc(tableName)); } table.setIndexes(indexes); tableList.add(table); } if (table != null) { boolean isLob = false; for (Method method : classObj.getMethods()) { String methodName = method.getName(); if (!methodName.startsWith("get")) { continue; } if (DEBUG) { System.out.println(className + " " + method.getName()); } Type type = method.getGenericReturnType(); Class<?> typeClass; if (type instanceof Class<?>) { typeClass = (Class<?>) type; } else if (type instanceof ParameterizedType) { typeClass = null; for (Type t : ((ParameterizedType) type).getActualTypeArguments()) { if (t instanceof Class<?>) { typeClass = (Class<?>) t; } } } else { if (!method.getName().equals("getDataObj") && !method.getName().equals("getTreeRootNode")) { log.warn("Not handled: " + type); } typeClass = null; } // rods 07/10/08 - Used to skip all relationships that point to themselves // that works now and is needed. if (typeClass == null || typeClass == AttributeIFace.class || typeClass == PickListItemIFace.class || typeClass == RecordSetItemIFace.class) { continue; } String thisSideName = getFieldNameFromMethod(method); if (method.isAnnotationPresent(javax.persistence.Lob.class)) { isLob = true; } if (method.isAnnotationPresent(javax.persistence.Column.class)) { if (method.isAnnotationPresent(javax.persistence.Id.class)) { table.addId(createId(method, (javax.persistence.Column) method .getAnnotation(javax.persistence.Column.class))); } else { Field field = createField(method, (javax.persistence.Column) method.getAnnotation(javax.persistence.Column.class), isLob); if (includeDesc) { field.setDesc(getFieldDesc(tableName, field.getName())); field.setNameDesc(getFieldNameDesc(tableName, field.getName())); } if (typeClass == java.util.Calendar.class) { String mName = method.getName() + "Precision"; for (Method mthd : classObj.getMethods()) { if (mthd.getName().equals(mName)) { field.setPartialDate(true); field.setDatePrecisionName(field.getName() + "Precision"); break; } } } table.addField(field); } } else if (method.isAnnotationPresent(javax.persistence.ManyToOne.class)) { javax.persistence.ManyToOne oneToMany = (javax.persistence.ManyToOne) method .getAnnotation(javax.persistence.ManyToOne.class); boolean isSave = false; for (CascadeType ct : oneToMany.cascade()) { if (ct == CascadeType.ALL || ct == CascadeType.PERSIST) { isSave = true; } } isSave = !isSave ? isOKToSave(method) : isSave; String otherSideName = getRightSideForManyToOne(classObj, typeClass, thisSideName); javax.persistence.JoinColumn join = method .isAnnotationPresent(javax.persistence.JoinColumn.class) ? (javax.persistence.JoinColumn) method .getAnnotation(javax.persistence.JoinColumn.class) : null; if (join != null) { //String othersideName = typeClass == null ? "" : getOthersideName(classObj, typeClass, thisSideName, RelType.OneToMany); Relationship rel = createRelationship(method, "many-to-one", join, otherSideName, join != null ? !join.nullable() : false); table.addRelationship(rel); rel.setSave(isSave); if (includeDesc) { rel.setDesc(getFieldDesc(tableName, rel.getRelationshipName())); rel.setNameDesc(getFieldNameDesc(tableName, rel.getRelationshipName())); } } else { log.error("No Join!"); } } else if (method.isAnnotationPresent(javax.persistence.ManyToMany.class)) { javax.persistence.ManyToMany manyToMany = method .getAnnotation(javax.persistence.ManyToMany.class); String othersideName = manyToMany.mappedBy(); if (StringUtils.isEmpty(othersideName)) { othersideName = getRightSideForManyToMany(classObj, typeClass, getFieldNameFromMethod(method)); } boolean isSave = false; for (CascadeType ct : manyToMany.cascade()) { if (ct == CascadeType.ALL || ct == CascadeType.PERSIST) { isSave = true; } } isSave = !isSave ? isOKToSave(method) : isSave; javax.persistence.JoinColumn join = method .isAnnotationPresent(javax.persistence.JoinColumn.class) ? (javax.persistence.JoinColumn) method .getAnnotation(javax.persistence.JoinColumn.class) : null; Relationship rel = createRelationship(method, "many-to-many", join, othersideName, join != null ? !join.nullable() : false); rel.setLikeManyToOne(table.getIsLikeManyToOne(rel.getRelationshipName())); rel.setSave(isSave); table.addRelationship(rel); if (includeDesc) { rel.setDesc(getFieldDesc(tableName, rel.getRelationshipName())); rel.setNameDesc(getFieldNameDesc(tableName, rel.getRelationshipName())); } javax.persistence.JoinTable joinTable = method .getAnnotation(javax.persistence.JoinTable.class); if (joinTable != null) { rel.setJoinTableName(joinTable.name()); } } else if (method.isAnnotationPresent(javax.persistence.OneToMany.class)) { javax.persistence.OneToMany oneToMany = (javax.persistence.OneToMany) method .getAnnotation(javax.persistence.OneToMany.class); String othersideName = oneToMany.mappedBy(); if (StringUtils.isEmpty(othersideName)) { // This Should never happen othersideName = getRightSideForOneToMany(classObj, typeClass, oneToMany.mappedBy()); } boolean isSave = false; for (CascadeType ct : oneToMany.cascade()) { if (ct == CascadeType.ALL || ct == CascadeType.PERSIST) { isSave = true; } } isSave = !isSave ? isOKToSave(method) : isSave; javax.persistence.JoinColumn join = method .isAnnotationPresent(javax.persistence.JoinColumn.class) ? (javax.persistence.JoinColumn) method .getAnnotation(javax.persistence.JoinColumn.class) : null; Relationship rel = createRelationship(method, "one-to-many", join, othersideName, join != null ? !join.nullable() : false); rel.setLikeManyToOne(table.getIsLikeManyToOne(rel.getRelationshipName())); rel.setSave(isSave); table.addRelationship(rel); if (includeDesc) { rel.setDesc(getFieldDesc(tableName, rel.getRelationshipName())); rel.setNameDesc(getFieldNameDesc(tableName, rel.getRelationshipName())); } } else if (method.isAnnotationPresent(javax.persistence.OneToOne.class)) { javax.persistence.OneToOne oneToOne = (javax.persistence.OneToOne) method .getAnnotation(javax.persistence.OneToOne.class); String leftSideVarName = getFieldNameFromMethod(method); boolean isMappedBy = true; String othersideName = oneToOne.mappedBy(); if (StringUtils.isEmpty(othersideName)) { isMappedBy = false; othersideName = getRightSideForOneToOne(classObj, typeClass, leftSideVarName, othersideName, isMappedBy); } boolean isSave = false; for (CascadeType ct : oneToOne.cascade()) { if (ct == CascadeType.ALL || ct == CascadeType.PERSIST) { isSave = true; } } isSave = !isSave ? isOKToSave(method) : isSave; javax.persistence.JoinColumn join = method .isAnnotationPresent(javax.persistence.JoinColumn.class) ? (javax.persistence.JoinColumn) method .getAnnotation(javax.persistence.JoinColumn.class) : null; Relationship rel = createRelationship(method, "one-to-one", join, othersideName, join != null ? !join.nullable() : false); rel.setSave(isSave); table.addRelationship(rel); if (includeDesc) { rel.setDesc(getFieldDesc(tableName, rel.getRelationshipName())); rel.setNameDesc(getFieldNameDesc(tableName, rel.getRelationshipName())); } } isLob = false; } // This updates each field as to whether it is an index table.updateIndexFields(); } } catch (Exception ex) { ex.printStackTrace(); } }
From source file:com.mmnaseri.dragonfly.metadata.impl.AnnotationTableMetadataResolver.java
private static CascadeMetadata getCascadeMetadata(Method method) { final List<CascadeType> cascadeTypes = new ArrayList<CascadeType>(); if (method.isAnnotationPresent(OneToOne.class)) { cascadeTypes.addAll(Arrays.asList(method.getAnnotation(OneToOne.class).cascade())); } else if (method.isAnnotationPresent(OneToMany.class)) { cascadeTypes.addAll(Arrays.asList(method.getAnnotation(OneToMany.class).cascade())); } else if (method.isAnnotationPresent(ManyToOne.class)) { cascadeTypes.addAll(Arrays.asList(method.getAnnotation(ManyToOne.class).cascade())); } else if (method.isAnnotationPresent(ManyToMany.class)) { cascadeTypes.addAll(Arrays.asList(method.getAnnotation(ManyToMany.class).cascade())); }/*from ww w. j a v a2 s .co m*/ final boolean cascadeAll = cascadeTypes.contains(CascadeType.ALL); return new ImmutableCascadeMetadata(cascadeAll || cascadeTypes.contains(CascadeType.PERSIST), cascadeAll || cascadeTypes.contains(CascadeType.MERGE), cascadeAll || cascadeTypes.contains(CascadeType.REMOVE), cascadeAll || cascadeTypes.contains(CascadeType.REFRESH)); }
From source file:org.grails.datastore.mapping.engine.NativeEntryEntityPersister.java
@Override protected final Serializable persistEntity(final PersistentEntity persistentEntity, Object obj) { T tmp = null;/*from w ww .ja va2s . co m*/ final NativeEntryModifyingEntityAccess entityAccess = (NativeEntryModifyingEntityAccess) createEntityAccess( persistentEntity, obj, tmp); K k = readObjectIdentifier(entityAccess, persistentEntity.getMapping()); boolean isUpdate = k != null; boolean assignedId = false; if (isUpdate && !getSession().isDirty(obj)) { return (Serializable) k; } PendingOperation<T, K> pendingOperation; PropertyMapping mapping = persistentEntity.getIdentity().getMapping(); if (mapping != null) { Property p = (Property) mapping.getMappedForm(); assignedId = p != null && "assigned".equals(p.getGenerator()); if (assignedId) { if (isUpdate && !session.contains(obj)) { isUpdate = false; } } } String family = getEntityFamily(); SessionImplementor<Object> si = (SessionImplementor<Object>) session; if (!isUpdate) { tmp = createNewEntry(family); if (!assignedId) k = generateIdentifier(persistentEntity, tmp); cacheNativeEntry(persistentEntity, (Serializable) k, tmp); pendingOperation = new PendingInsertAdapter<T, K>(persistentEntity, k, tmp, entityAccess) { public void run() { executeInsert(persistentEntity, entityAccess, getNativeKey(), getNativeEntry()); } }; entityAccess.setProperty(entityAccess.getIdentifierName(), k); } else { tmp = (T) si.getCachedEntry(persistentEntity, (Serializable) k); if (tmp == null) { tmp = getFromTPCache(persistentEntity, (Serializable) k); if (tmp == null) { tmp = retrieveEntry(persistentEntity, family, (Serializable) k); } } if (tmp == null) { tmp = createNewEntry(family); } final T finalTmp = tmp; final K finalK = k; pendingOperation = new PendingUpdateAdapter<T, K>(persistentEntity, finalK, finalTmp, entityAccess) { public void run() { if (cancelUpdate(persistentEntity, entityAccess)) return; updateEntry(persistentEntity, entityAccess, getNativeKey(), getNativeEntry()); updateTPCache(persistentEntity, finalTmp, (Serializable) finalK); firePostUpdateEvent(persistentEntity, entityAccess); } }; } final T e = tmp; entityAccess.setNativeEntry(e); final List<PersistentProperty> props = persistentEntity.getPersistentProperties(); final Map<Association, List<Serializable>> toManyKeys = new HashMap<Association, List<Serializable>>(); final Map<OneToMany, Serializable> inverseCollectionUpdates = new HashMap<OneToMany, Serializable>(); final Map<PersistentProperty, Object> toIndex = new HashMap<PersistentProperty, Object>(); final Map<PersistentProperty, Object> toUnindex = new HashMap<PersistentProperty, Object>(); entityAccess.setToIndex(toIndex); for (PersistentProperty prop : props) { PropertyMapping<Property> pm = prop.getMapping(); final Property mappedProperty = pm.getMappedForm(); String key = null; if (mappedProperty != null) { key = mappedProperty.getTargetName(); } if (key == null) key = prop.getName(); final boolean indexed = isPropertyIndexed(mappedProperty); if ((prop instanceof Simple) || (prop instanceof Basic)) { Object propValue = entityAccess.getProperty(prop.getName()); handleIndexing(isUpdate, e, toIndex, toUnindex, prop, key, indexed, propValue); setEntryValue(e, key, propValue); } else if ((prop instanceof Custom)) { CustomTypeMarshaller customTypeMarshaller = ((Custom) prop).getCustomTypeMarshaller(); if (customTypeMarshaller.supports(getSession().getDatastore())) { Object propValue = entityAccess.getProperty(prop.getName()); Object customValue = customTypeMarshaller.write(prop, propValue, e); handleIndexing(isUpdate, e, toIndex, toUnindex, prop, key, indexed, customValue); } } 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)) { EntityPersister associationPersister = (EntityPersister) session .getPersister(oneToMany.getAssociatedEntity()); if (associationPersister != null) { PersistentCollection persistentCollection; boolean newCollection = false; if (associatedObjects instanceof PersistentCollection) { persistentCollection = (PersistentCollection) associatedObjects; } else { Class associationType = oneToMany.getAssociatedEntity().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, e, 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, e); } else if (association.doesCascade(CascadeType.PERSIST)) { final Object associatedObject = entityAccess.getProperty(prop.getName()); if (associatedObject != null) { @SuppressWarnings("hiding") ProxyFactory proxyFactory = getProxyFactory(); // never cascade to proxies if (proxyFactory.isInitialized(associatedObject)) { Serializable associationId = null; NativeEntryEntityPersister associationPersister = (NativeEntryEntityPersister) session .getPersister(associatedObject); if (!session.contains(associatedObject)) { Serializable tempId = associationPersister.getObjectIdentifier(associatedObject); if (tempId == null) { if (association.isOwningSide()) { tempId = session.persist(associatedObject); } } associationId = tempId; } else { associationId = associationPersister.getObjectIdentifier(associatedObject); } // handling of hasOne inverse key if (association.isForeignKeyInChild()) { T cachedAssociationEntry = (T) 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) { if (indexed && doesRequirePropertyIndexing()) { toIndex.put(prop, associationId); if (isUpdate) { Object oldValue = getEntryValue(e, key); oldValue = oldValue != null ? convertToNativeKey((Serializable) oldValue) : oldValue; if (oldValue != null && !oldValue.equals(associationId)) { toUnindex.put(prop, oldValue); } } } setEntryValue(e, key, formulateDatabaseReference(persistentEntity, association, associationId)); if (association.isBidirectional()) { Association inverse = association.getInverseSide(); if (inverse instanceof OneToMany) { inverseCollectionUpdates.put((OneToMany) inverse, associationId); } Object inverseEntity = entityAccess.getProperty(association.getName()); if (inverseEntity != null) { EntityAccess inverseAccess = createEntityAccess( association.getAssociatedEntity(), inverseEntity); if (inverse instanceof OneToMany) { Collection existingValues = (Collection) inverseAccess .getProperty(inverse.getName()); if (existingValues == null) { existingValues = MappingUtils .createConcreteCollection(inverse.getType()); inverseAccess.setProperty(inverse.getName(), existingValues); } existingValues.add(entityAccess.getEntity()); } else if (inverse instanceof ToOne) { inverseAccess.setProperty(inverse.getName(), entityAccess.getEntity()); } } } } } } } else { setEntryValue(e, getPropertyKey(prop), null); } } } else if (prop instanceof EmbeddedCollection) { handleEmbeddedToMany(entityAccess, e, prop, key); } } if (!isUpdate) { // if the identifier is null at this point that means that datastore could not generated an identifer // and the identifer is generated only upon insert of the entity final K updateId = k; PendingOperation postOperation = new PendingOperationAdapter<T, K>(persistentEntity, k, e) { public void run() { updateToManyIndices(e, updateId, toManyKeys); if (doesRequirePropertyIndexing()) { toIndex.put(persistentEntity.getIdentity(), updateId); updatePropertyIndices(updateId, toIndex, toUnindex); } for (OneToMany inverseCollection : inverseCollectionUpdates.keySet()) { final Serializable primaryKey = inverseCollectionUpdates.get(inverseCollection); final NativeEntryEntityPersister inversePersister = (NativeEntryEntityPersister) session .getPersister(inverseCollection.getOwner()); final AssociationIndexer associationIndexer = inversePersister.getAssociationIndexer(e, inverseCollection); associationIndexer.index(primaryKey, updateId); } } }; pendingOperation.addCascadeOperation(postOperation); // If the key is still null at this point we have to execute the pending operation now to get the key if (k == null) { PendingOperationExecution.executePendingOperation(pendingOperation); } else { si.addPendingInsert((PendingInsert) pendingOperation); } } else { final K updateId = k; PendingOperation postOperation = new PendingOperationAdapter<T, K>(persistentEntity, k, e) { public void run() { updateToManyIndices(e, updateId, toManyKeys); if (doesRequirePropertyIndexing()) { updatePropertyIndices(updateId, toIndex, toUnindex); } } }; pendingOperation.addCascadeOperation(postOperation); si.addPendingUpdate((PendingUpdate) pendingOperation); } return (Serializable) k; }
From source file:edu.ku.brc.specify.tools.datamodelgenerator.DatamodelGenerator.java
/** * @param method/*from w w w . j a v a 2 s. co m*/ * @return */ @SuppressWarnings("cast") protected boolean isOKToSave(final Method method) { org.hibernate.annotations.Cascade hibCascade = (org.hibernate.annotations.Cascade) method .getAnnotation(org.hibernate.annotations.Cascade.class); if (hibCascade != null) { for (org.hibernate.annotations.CascadeType ct : hibCascade.value()) { if (ct == org.hibernate.annotations.CascadeType.ALL || ct == org.hibernate.annotations.CascadeType.PERSIST || ct == org.hibernate.annotations.CascadeType.SAVE_UPDATE) { return true; } } } return false; }
From source file:org.grails.datastore.mapping.engine.NativeEntryEntityPersister.java
private void updateToManyIndices(T nativeEntry, Object identifier, Map<Association, List<Serializable>> toManyKeys) { // now cascade onto one-to-many associations for (Association association : toManyKeys.keySet()) { if (association.doesCascade(CascadeType.PERSIST)) { final AssociationIndexer indexer = getAssociationIndexer(nativeEntry, association); if (indexer != null) { indexer.index(identifier, toManyKeys.get(association)); }/* ww w. j av a2 s . co m*/ } } }