Example usage for javax.persistence CascadeType REMOVE

List of usage examples for javax.persistence CascadeType REMOVE

Introduction

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

Prototype

CascadeType REMOVE

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

Click Source Link

Document

Cascade remove operation

Usage

From source file:com.sammyun.entity.Member.java

/**
 * @return ?// w w  w  . ja v  a 2  s. c  o m
 */
@OneToMany(mappedBy = "member", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
public Set<WorkScheduling> getWorkSchedulings() {
    return workSchedulings;
}

From source file:com.dp2345.entity.Member.java

/**
 * ??/* ww  w  .  j  av  a 2  s .  com*/
 * 
 * @return ?
 */
@OneToMany(mappedBy = "member", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
public Set<CouponCode> getCouponCodes() {
    return couponCodes;
}

From source file:net.shopxx.entity.Goods.java

@OneToMany(mappedBy = "goods", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
public Set<Review> getReviews() {
    return reviews;
}

From source file:net.shopxx.entity.Member.java

@OneToMany(mappedBy = "member", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
public Set<VirAccount> getVirAccounts() {
    return virAccounts;
}

From source file:net.shopxx.entity.Goods.java

@OneToMany(mappedBy = "goods", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
public Set<Consultation> getConsultations() {
    return consultations;
}

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

/**
 * @param parent// w w w.  j  a  v  a 2  s. c  o m
 * @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:com.dp2345.entity.Member.java

/**
 * ??/*  w  ww. j a  v a2s  .c  om*/
 * 
 * @return ?
 */
@OneToMany(mappedBy = "member", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@OrderBy("isDefault desc, createDate desc")
public Set<Receiver> getReceivers() {
    return receivers;
}

From source file:net.shopxx.entity.Goods.java

@OneToMany(mappedBy = "goods", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
public Set<Product> getProducts() {
    return products;
}

From source file:com.dp2345.entity.Member.java

/**
 * ?//w  ww.j a v a 2  s . c o  m
 * 
 * @return 
 */
@OneToMany(mappedBy = "member", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@OrderBy("createDate desc")
public Set<Review> getReviews() {
    return reviews;
}

From source file:com.sammyun.entity.Member.java

/**
 * ?//  w  ww.ja va2  s .c  o  m
 * 
 * @return teacherAttendances
 * @see [?#?#?]
 */
@OneToMany(mappedBy = "member", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
public Set<TeacherAttendance> getTeacherAttendances() {
    return teacherAttendances;
}