List of usage examples for javax.persistence CascadeType ALL
CascadeType ALL
To view the source code for javax.persistence CascadeType ALL.
Click Source Link
From source file:com.denimgroup.threadfix.data.entities.Application.java
@OneToMany(mappedBy = "application", cascade = CascadeType.ALL) @JsonIgnore//from w w w . j av a 2s .c om public List<ApplicationVersion> getVersions() { if (versions != null) { Collections.sort(versions); } return versions; }
From source file:org.finra.herd.dao.impl.AbstractHerdDao.java
/** * Updates the audit fields if the entity is of type AuditableEntity. * * @param entity the entity/* ww w . j a v a2s . c o m*/ * @param <T> the type of entity */ @SuppressWarnings("rawtypes") private <T> void updateAuditFields(T entity) { if (entity instanceof AuditableEntity) { AuditableEntity auditableEntity = (AuditableEntity) entity; // Get the currently logged in username. String username = herdDaoSecurityHelper.getCurrentUsername(); // Always set the updated by field, but only set the created by field when it is null (i.e. this is a new record). if (auditableEntity.getCreatedBy() == null) { auditableEntity.setCreatedBy(username); } auditableEntity.setUpdatedBy(username); // Always set the updated on field to the current time, but only update the created on field when it is null (i.e. the first time). Timestamp currentTime = new Timestamp(System.currentTimeMillis()); auditableEntity.setUpdatedOn(currentTime); if (auditableEntity.getCreatedOn() == null) { auditableEntity.setCreatedOn(currentTime); } } // Try to update children one-to-many cascadable auditable entities. // Note that this assumes that OneToMany annotations are done on the field (as opposed to the method) and that all OneToMany fields are collections. // This approach also assumes that there are loops where children refer back to our entity (i.e. an infinite loop). // If there are other scenarios, we should modify this code to handle them. // Loop through all the fields of this entity. for (Field field : entity.getClass().getDeclaredFields()) { // Get all the annotations for the field. for (Annotation annotation : field.getDeclaredAnnotations()) { // Only look for OneToMany that cascade with "persist" or "merge". if (annotation instanceof OneToMany) { OneToMany oneToManyAnnotation = (OneToMany) annotation; List<CascadeType> cascadeTypes = new ArrayList<>(Arrays.asList(oneToManyAnnotation.cascade())); if ((cascadeTypes.contains(CascadeType.ALL)) || (cascadeTypes.contains(CascadeType.PERSIST)) || cascadeTypes.contains(CascadeType.MERGE)) { try { // Modify the accessibility to true so we can get the field (even if it's private) and get the value of the field for our entity. field.setAccessible(true); Object fieldValue = field.get(entity); // If the field is a collection (which OneToMany annotated fields should be), then iterate through the collection and look for // child auditable entities. if (fieldValue instanceof Collection) { Collection collection = (Collection) fieldValue; for (Object object : collection) { if (object instanceof AuditableEntity) { // We found a child auditable entity so recurse to update it's audit fields as well. updateAuditFields(object); } } } } catch (IllegalAccessException ex) { // Because we're setting accessible to true above, we shouldn't get here. throw new IllegalStateException("Unable to get field value for field \"" + field.getName() + "\" due to access restriction.", ex); } } } } } }
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 www . j a v a 2 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:edu.ku.brc.specify.dbsupport.SpecifyDeleteHelper.java
/** * @param method/*from www . j a v a 2s . c om*/ * @return */ protected boolean isOKToDel(final Method method) { org.hibernate.annotations.Cascade hibCascade = (org.hibernate.annotations.Cascade) method .getAnnotation(org.hibernate.annotations.Cascade.class); if (hibCascade != null) { boolean isAllOrDel = false; for (org.hibernate.annotations.CascadeType ct : hibCascade.value()) { if (ct == org.hibernate.annotations.CascadeType.ALL || ct == org.hibernate.annotations.CascadeType.DELETE) { isAllOrDel = true; } else if (isAllOrDel && ct == org.hibernate.annotations.CascadeType.DELETE_ORPHAN) { return true; } } } return false; }
From source file:org.apache.openjpa.persistence.AnnotationPersistenceMetaDataSerializer.java
/** * Serialize cascades./*from w w w. ja v a2 s . co m*/ */ private void serializeCascades(ValueMetaData vmd, AnnotationBuilder ab) { EnumSet<CascadeType> cascades = EnumSet.noneOf(CascadeType.class); if (vmd.getCascadePersist() == ValueMetaData.CASCADE_IMMEDIATE) { cascades.add(CascadeType.PERSIST); } if (vmd.getCascadeAttach() == ValueMetaData.CASCADE_IMMEDIATE) { cascades.add(CascadeType.MERGE); } if (vmd.getCascadeDelete() == ValueMetaData.CASCADE_IMMEDIATE) { cascades.add(CascadeType.REMOVE); } if (vmd.getCascadeRefresh() == ValueMetaData.CASCADE_IMMEDIATE) { cascades.add(CascadeType.REFRESH); } if (vmd.getCascadeDetach() == ValueMetaData.CASCADE_IMMEDIATE) { cascades.add(CascadeType.DETACH); } if (cascades.size() == 5) // ALL { cascades.clear(); cascades.add(CascadeType.ALL); } if (!cascades.isEmpty()) { ab.add("cascade", cascades); } }
From source file:edu.ku.brc.specify.tools.datamodelgenerator.DatamodelGenerator.java
/** * @param method/*from w ww .ja v a2s .c om*/ * @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:net.groupbuy.entity.Product.java
/** * ??//from w w w. j a v a 2 s . c om * * @return ? */ @OneToMany(mappedBy = "gift", fetch = FetchType.LAZY, cascade = CascadeType.ALL) public Set<GiftItem> getGiftItems() { return giftItems; }
From source file:org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.java
/** * Set cascades on relation.//from ww w . j a va 2s. c o m */ private void setCascades(ValueMetaData vmd, CascadeType[] cascades) { for (CascadeType cascade : cascades) { if (cascade == CascadeType.ALL || cascade == CascadeType.REMOVE) vmd.setCascadeDelete(ValueMetaData.CASCADE_IMMEDIATE); if (cascade == CascadeType.ALL || cascade == CascadeType.PERSIST) vmd.setCascadePersist(ValueMetaData.CASCADE_IMMEDIATE); if (cascade == CascadeType.ALL || cascade == CascadeType.MERGE) vmd.setCascadeAttach(ValueMetaData.CASCADE_IMMEDIATE); if (cascade == CascadeType.ALL || cascade == CascadeType.DETACH) vmd.setCascadeDetach(ValueMetaData.CASCADE_IMMEDIATE); if (cascade == CascadeType.ALL || cascade == CascadeType.REFRESH) vmd.setCascadeRefresh(ValueMetaData.CASCADE_IMMEDIATE); } }
From source file:org.finra.dm.dao.impl.DmDaoImpl.java
/** * Updates the audit fields if the entity is of type AuditableEntity. * * @param entity the entity/*w w w.j a v a 2 s . co m*/ * @param <T> the type of entity */ @SuppressWarnings("rawtypes") private <T> void updateAuditFields(T entity) { if (entity instanceof AuditableEntity) { AuditableEntity auditableEntity = (AuditableEntity) entity; // Get the currently logged in username. String username = dmDaoSecurityHelper.getCurrentUsername(); // Always set the updated by field, but only set the created by field when it is null (i.e. this is a new record). if (auditableEntity.getCreatedBy() == null) { auditableEntity.setCreatedBy(username); } auditableEntity.setUpdatedBy(username); // Always set the updated on field to the current time, but only update the created on field when it is null (i.e. the first time). Timestamp currentTime = new Timestamp(System.currentTimeMillis()); auditableEntity.setUpdatedOn(currentTime); if (auditableEntity.getCreatedOn() == null) { auditableEntity.setCreatedOn(currentTime); } } // Try to update children one-to-many cascadable auditable entities. // Note that this assumes that OneToMany annotations are done on the field (as opposed to the method) and that all OneToMany fields are collections. // This approach also assumes that there are loops where children refer back to our entity (i.e. an infinite loop). // If there are other scenarios, we should modify this code to handle them. // Loop through all the fields of this entity. for (Field field : entity.getClass().getDeclaredFields()) { // Get all the annotations for the field. for (Annotation annotation : field.getDeclaredAnnotations()) { // Only look for OneToMany that cascade with "persist" or "merge". if (annotation instanceof OneToMany) { OneToMany oneToManyAnnotation = (OneToMany) annotation; List<CascadeType> cascadeTypes = new ArrayList<>(Arrays.asList(oneToManyAnnotation.cascade())); if ((cascadeTypes.contains(CascadeType.ALL)) || (cascadeTypes.contains(CascadeType.PERSIST)) || cascadeTypes.contains(CascadeType.MERGE)) { try { // Modify the accessibility to true so we can get the field (even if it's private) and get the value of the field for our entity. field.setAccessible(true); Object fieldValue = field.get(entity); // If the field is a collection (which OneToMany annotated fields should be), then iterate through the collection and look for // child auditable entities. if (fieldValue instanceof Collection) { Collection collection = (Collection) fieldValue; for (Object object : collection) { if (object instanceof AuditableEntity) { // We found a child auditable entity so recurse to update it's audit fields as well. updateAuditFields(object); } } } } catch (IllegalAccessException ex) { // Because we're setting accessible to true above, we shouldn't get here. throw new IllegalStateException("Unable to get field value for field \"" + field.getName() + "\" due to access restriction.", ex); } } } } } }