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:org.opennms.netmgt.model.OnmsNode.java

/**
 * The interfaces on this node/*from   w w w  .  j  a va  2  s  . c  om*/
 *
 * @return a {@link java.util.Set} object.
 */
@XmlTransient
@OneToMany(mappedBy = "node", orphanRemoval = true)
@org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.ALL)
public Set<OnmsIpInterface> getIpInterfaces() {
    return m_ipInterfaces;
}

From source file:com.doculibre.constellio.entities.RecordCollection.java

@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(name = "RecordCollection_Labels", joinColumns = {
        @JoinColumn(name = "recordCollection_id") }, inverseJoinColumns = { @JoinColumn(name = "label_id") })
public Set<I18NLabel> getLabels() {
    return this.labels;
}

From source file:com.medigy.persist.model.person.Person.java

/**
 * Gets all the insurance policies of this person. This will include policies that have expired also.
 *
 * @return a set of insurance policies//ww w .j a va  2s .  c  om
 */
@OneToMany(cascade = CascadeType.ALL, mappedBy = "insuredPerson")
public List<InsurancePolicy> getInsurancePolicies() {
    return insurancePolicies;
}

From source file:org.opennms.netmgt.model.OnmsNode.java

/**
 * The information from the SNMP interfaces/ipAddrTables for the node
 *
 * @return a {@link java.util.Set} object.
 *///from  w w w .  ja  v  a  2s . c om
@XmlTransient
@OneToMany(mappedBy = "node", orphanRemoval = true)
@org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.ALL)
public Set<OnmsSnmpInterface> getSnmpInterfaces() {
    return m_snmpInterfaces;
}

From source file:edu.ku.brc.specify.dbsupport.SpecifyDeleteHelper.java

/**
 * @param parent/*  w  ww  . j  a  v  a 2  s  .  c om*/
 * @param cls
 * @param sqlStr
 * @param delSqlStr
 * @param level
 */
protected StackItem getSubTables(final StackItem parent, final Class<?> cls, final int id, final String sqlStr,
        final String delSqlStr, final int level, final Hashtable<String, Boolean> inUseHashArg,
        final boolean checkIfIsShared/*,
                                     final int excludeId /*check if is required by anything other than this id*/) {
    if (classHash.contains(cls)) {
        return null;
    }
    classHash.add(cls);

    if (debug) {
        printLevel(level);
        System.out.println(cls.getSimpleName());
    }

    DBTableInfo tblInfo = tblMgr.getByShortClassName(cls.getSimpleName());

    StackItem child = parent.push(tblInfo, sqlStr, delSqlStr);

    Hashtable<String, Boolean> inUseHash = inUseHashArg == null && level == 1 ? new Hashtable<String, Boolean>()
            : inUseHashArg;

    for (Method method : cls.getMethods()) {
        String methodName = method.getName();
        //System.out.println(methodName);

        // Skip if it is a not a getter
        if (!methodName.startsWith("get")) {
            continue;
        }

        // Skip if it is a not a ManyToOne
        if (method.isAnnotationPresent(javax.persistence.Transient.class)) {
            continue;
        }

        if (methodName.endsWith("TreeDef")) {
            if (doTrees) {
                String className = methodName.substring(3, methodName.length() - 7);

                String tableNameTD = className.toLowerCase() + "treedef";
                String primaryKeyTD = className + "TreeDefID";
                String itemTableNameTDI = className.toLowerCase() + "treedefitem";

                String sql;
                String delSql;

                try {
                    sql = "SELECT " + primaryKeyTD + " FROM " + tblInfo.getName() + " WHERE "
                            + tblInfo.getIdColumnName() + " = " + id;
                    if (debugUpdate)
                        System.err.println(sql);

                    Vector<Integer> ids = getIds(sql, level);
                    if (ids != null && ids.size() > 0) {
                        Class<?> treeClass = null;
                        try {
                            treeClass = Class.forName("edu.ku.brc.specify.datamodel." + className);
                        } catch (Exception ex) {
                            ex.printStackTrace();
                            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
                            edu.ku.brc.exceptions.ExceptionTracker.getInstance()
                                    .capture(SpecifyDeleteHelper.class, ex);
                        }
                        if (treeClass == cls) {
                            classHash.remove(cls);
                            return null;
                        }

                        if (treeClass == Taxon.class) {
                            String tmpSql = "SELECT tc.TaxonCitationID FROM taxoncitation tc INNER JOIN taxon tx ON tc.TaxonID = tx.TaxonID INNER JOIN taxontreedef ttd ON tx.TaxonTreeDefID = ttd.TaxonTreeDefID = "
                                    + ids.get(0);
                            delSql = "DELETE FROM taxoncitation WHERE TaxonCitationID = ";
                            child.pushPPS(new StackItem(null, tmpSql, delSql, false, true));
                        }

                        delSql = "DELETE FROM " + className.toLowerCase() + " WHERE " + primaryKeyTD + " = "
                                + ids.get(0) + " ORDER BY AcceptedID DESC, ParentID DESC";
                        child.pushPPS(new StackItem(null, sql, delSql, false, false));

                        delSql = "DELETE FROM " + itemTableNameTDI + " WHERE " + primaryKeyTD + " = "
                                + ids.get(0) + " ORDER BY RankID DESC";
                        child.pushPPS(new StackItem(null, sql, delSql, false, false));

                        delSql = "DELETE FROM " + tableNameTD + " WHERE " + primaryKeyTD + " = " + ids.get(0);
                        child.pushPPS(new StackItem(null, sql, delSql, false, false));
                    }

                } catch (SQLException ex) {
                    edu.ku.brc.af.core.UsageTracker.incrSQLUsageCount();
                    edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(SpecifyDeleteHelper.class, ex);
                    ex.printStackTrace();
                }
            }
            continue;
        }

        String colName = null;
        boolean isOKToDel = false;
        boolean includeSubTable = false;
        if (method.isAnnotationPresent(javax.persistence.OneToMany.class)) {
            String nm = method.getName();
            boolean isAttachment = nm.indexOf("Attachment") > -1;

            boolean doDel = false;
            javax.persistence.OneToMany oneToMany = (javax.persistence.OneToMany) method
                    .getAnnotation(javax.persistence.OneToMany.class);
            for (CascadeType ct : oneToMany.cascade()) {
                if (ct == CascadeType.ALL || ct == CascadeType.REMOVE) {
                    doDel = true;
                    break;
                }
            }

            if (!isAttachment && !doDel
                    && method.isAnnotationPresent(org.hibernate.annotations.Cascade.class)) {
                org.hibernate.annotations.Cascade cascade = (org.hibernate.annotations.Cascade) method
                        .getAnnotation(org.hibernate.annotations.Cascade.class);
                for (org.hibernate.annotations.CascadeType ct : cascade.value()) {
                    if (ct == org.hibernate.annotations.CascadeType.ALL
                            || ct == org.hibernate.annotations.CascadeType.DELETE
                            || ct == org.hibernate.annotations.CascadeType.REMOVE) {
                        doDel = true;
                        break;
                    }
                }
            }

            isOKToDel = !doDel ? isOKToDel(method) : true;
            if (checkIfIsShared && cls.equals(Geography.class) && method.getName().equals("getLocalities")) //Hibernate!?. Looks like the Cascade annotation is wrong.
            {
                isOKToDel = false;
            }
            includeSubTable = !checkIfIsShared ? isOKToDel : !isOKToDel;
            colName = tblInfo.getIdColumnName();

        } else if (!checkIfIsShared && method.isAnnotationPresent(javax.persistence.ManyToOne.class)) {
            boolean doDel = false;
            javax.persistence.ManyToOne oneToMany = (javax.persistence.ManyToOne) method
                    .getAnnotation(javax.persistence.ManyToOne.class);
            for (CascadeType ct : oneToMany.cascade()) {
                if (ct == CascadeType.ALL || ct == CascadeType.REMOVE) {
                    doDel = true;
                }
            }
            isOKToDel = !doDel ? isOKToDel(method) : true;
            includeSubTable = !checkIfIsShared ? isOKToDel : !isOKToDel;
            if (includeSubTable) {
                javax.persistence.JoinColumn joinCol = (javax.persistence.JoinColumn) method
                        .getAnnotation(javax.persistence.JoinColumn.class);
                if (joinCol != null) {
                    colName = joinCol.name();
                }
            }

        } else if (method.isAnnotationPresent(javax.persistence.ManyToMany.class)) {
            javax.persistence.JoinTable joinTable = (javax.persistence.JoinTable) method
                    .getAnnotation(javax.persistence.JoinTable.class);
            if (joinTable != null) {
                String joinTableName = joinTable.name();
                String joinColName = null;
                for (JoinColumn jc : joinTable.joinColumns()) {
                    joinColName = jc.name();
                    break;
                }

                DBRelationshipInfo relInfo = null;
                for (DBRelationshipInfo ri : tblInfo.getRelationships()) {
                    if (ri.getJoinTable() != null && ri.getJoinTable().equals(joinTableName)) {
                        relInfo = ri;
                        break;
                    }
                }
                System.out.println(joinColName);

                if (cls != Agent.class) {
                    if (inUseHash != null)
                        inUseHash.put(relInfo.getClassName(), true);

                    String sql = "SELECT " + joinColName + " FROM " + joinTableName + " WHERE " + joinColName
                            + " = ";
                    String delSql = "DELETE FROM " + joinTableName + " WHERE " + joinColName + " = ";

                    if (debug) {
                        printLevel(level);
                        System.out.println(sql);
                    }

                    DBTableInfo ti = tblMgr.getByShortClassName(relInfo.getDataClass().getSimpleName());
                    if (!checkIfIsShared) {
                        child.push(ti, sql, delSql);
                    }

                } else {
                    //System.err.println(cls.getName());
                }

            }
        }

        if (includeSubTable) {
            //System.out.println(method.getName()+"  "+method.getReturnType().getSimpleName());
            String relName = method.getName().substring(3);
            DBRelationshipInfo relInfo = tblInfo.getRelationshipByName(relName);
            if (relInfo != null) {
                DBTableInfo ti = tblMgr.getByClassName(relInfo.getClassName());
                if (ti != null) {
                    String sql;
                    if (method.isAnnotationPresent(javax.persistence.OneToMany.class)) {
                        String otherColName = colName;
                        DBRelationshipInfo ri = ti.getRelationshipByName(relInfo.getOtherSide());
                        if (ri != null) {
                            otherColName = ri.getColName();
                        }
                        sql = "SELECT " + ti.getAbbrev() + "." + ti.getIdColumnName() + " FROM " + ti.getName()
                                + " " + ti.getAbbrev() + " INNER JOIN " + tblInfo.getName() + " "
                                + tblInfo.getAbbrev() + " ON " + ti.getAbbrev() + "." + otherColName + " = "
                                + tblInfo.getAbbrev() + "." + tblInfo.getIdColumnName() + "  WHERE "
                                + tblInfo.getAbbrev() + "." + colName + " = ";
                    } else {
                        sql = "SELECT " + ti.getAbbrev() + "." + ti.getIdColumnName() + " FROM " + ti.getName()
                                + " " + ti.getAbbrev() + " INNER JOIN " + tblInfo.getName() + " "
                                + tblInfo.getAbbrev() + " ON " + ti.getAbbrev() + "." + ti.getIdColumnName()
                                + " = " + tblInfo.getAbbrev() + "." + colName + "  WHERE " + tblInfo.getAbbrev()
                                + "." + tblInfo.getIdColumnName() + " = ";
                    }
                    String delSql = "DELETE FROM " + ti.getName() + " WHERE " + ti.getIdColumnName() + " = ";

                    if (debug) {
                        printLevel(level);
                        System.out.println(sql);
                        printLevel(level);
                        System.out.println(delSql);
                    }

                    if (relInfo.getDataClass() != Agent.class) {
                        if (inUseHash != null)
                            inUseHash.put(relInfo.getClassName(), true);

                        if (ti.getClassObj() != cls || (doTrees && !Treeable.class.isAssignableFrom(cls))) {
                            if (!checkIfIsShared) {
                                getSubTables(child, ti.getClassObj(), id, sql, delSql, level + 1, inUseHash,
                                        checkIfIsShared);
                            } else {
                                child.push(ti, sql, delSql);
                            }

                        } else if (debug) {
                            System.err.println("Skipping " + ti.getClassObj().getSimpleName());
                        }
                    } else {
                        //System.err.println(relInfo.getDataClass().getName());
                    }

                } else {
                    String shortClassName = relInfo.getDataClass().getSimpleName();
                    String sql = "SELECT " + shortClassName + "ID FROM " + shortClassName.toLowerCase()
                            + " WHERE " + tblInfo.getClassObj().getSimpleName() + "ID = ";
                    String delSql = "DELETE FROM " + shortClassName.toLowerCase() + " WHERE " + shortClassName
                            + "ID = ";
                    if (debug) {
                        printLevel(level);
                        System.out.println(sql);
                        printLevel(level);
                        System.out.println(delSql);
                    }
                    child.push(tblInfo, sql, delSql); // NOTE: the tblInfo is for the parent!
                }
            }
        }
    }

    for (DBTableInfo ti : tblMgr.getTables()) {
        if (ti != tblInfo) {
            for (DBRelationshipInfo ri : ti.getRelationships()) {
                /*System.out.println(ri.getName());*/

                if (ri.getDataClass() != Agent.class) {
                    boolean hashOK = inUseHash == null || inUseHash.get(ti.getClassName()) == null;
                    if (ri.getDataClass() == tblInfo.getClassObj() && hashOK
                            && StringUtils.isEmpty(ri.getOtherSide())) {
                        String sql = "SELECT " + ti.getIdColumnName() + " FROM " + ti.getName() + "  WHERE "
                                + ri.getColName() + " = ";
                        String delSql = "DELETE FROM " + ti.getName() + "  WHERE " + ti.getIdColumnName()
                                + " = ";

                        if (debug) {
                            printLevel(level);

                            System.out.println("Missed " + ti.getClassName() + " for " + tblInfo.getClassObj());
                            printLevel(level);
                            System.out.println(sql);
                        }

                        if (inUseHash != null)
                            inUseHash.put(ti.getClassName(), true);
                        //if (!checkIfIsShared)
                        {
                            getSubTables(child, ti.getClassObj(), id, sql, delSql, level + 1, inUseHash,
                                    checkIfIsShared);
                        }

                    } else if (ri.getDataClass() == tblInfo.getClassObj() && !hashOK
                            && StringUtils.isEmpty(ri.getOtherSide())) {
                        if (debug)
                            System.out.println("Skipping " + ti.getClassObj().getSimpleName() + " for "
                                    + tblInfo.getClassObj().getSimpleName());
                    }
                } else {
                    //System.err.println(ri.getDataClass().getName());
                }
            }
        }
    }

    if (debug)
        System.out.println();

    classHash.remove(cls);
    return child;
}

From source file:org.projectforge.business.fibu.AuftragDO.java

/**
 * Get the payment schedule entries for this object.
 *//*ww w . j  a va  2s .  com*/
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "auftrag")
@IndexColumn(name = "number", base = 1)
public List<PaymentScheduleDO> getPaymentSchedules() {
    return this.paymentSchedules;
}

From source file:org.opennms.netmgt.model.OnmsNode.java

/**
 * The ARP interfaces with this node as a source
 *
 * @return a {@link java.util.Set} object.
 *///  w  w  w. j a  v a 2 s  .  c  om
@XmlTransient
@OneToMany(mappedBy = "sourceNode", orphanRemoval = true)
@org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.ALL)
public Set<OnmsArpInterface> getArpInterfacesBySource() {
    return m_arpInterfacesBySource;
}

From source file:com.medigy.persist.model.person.Person.java

/**
 * Gets all the insurance policies to which this person is financially responsible for. This will also include
 * his/her own policies which they are responsible for.
 * @return a set of insurance policies/* w  ww  .ja va2s.  c  o  m*/
 */
@OneToMany(cascade = CascadeType.ALL, mappedBy = "contractHolderPerson", fetch = FetchType.LAZY)
public Set<InsurancePolicy> getResponsibleInsurancePolicies() {
    return responsibleInsurancePolicies;
}

From source file:edu.harvard.med.screensaver.model.libraries.Library.java

@OneToMany(mappedBy = "library", cascade = { CascadeType.ALL }, orphanRemoval = true)
@org.hibernate.annotations.Sort(type = org.hibernate.annotations.SortType.NATURAL)
public SortedSet<LibraryContentsVersion> getContentsVersions() {
    return _contentsVersions;
}

From source file:org.orcid.persistence.jpa.entities.ProfileEntity.java

@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
@JoinColumn(name = "group_orcid")
@Sort(type = SortType.COMPARATOR, comparator = OrcidEntityIdComparator.class)
public SortedSet<ClientDetailsEntity> getClients() {
    return clients;
}