Example usage for org.hibernate.annotations OnDeleteAction CASCADE

List of usage examples for org.hibernate.annotations OnDeleteAction CASCADE

Introduction

In this page you can find the example usage for org.hibernate.annotations OnDeleteAction CASCADE.

Prototype

OnDeleteAction CASCADE

To view the source code for org.hibernate.annotations OnDeleteAction CASCADE.

Click Source Link

Document

Use cascade delete capabilities of the database foreign-key.

Usage

From source file:com.github.gekoh.yagen.ddl.TableConfig.java

License:Apache License

private void gatherCascade(final Map<String, String> attr2colName, final String attrPath, Class type) {
    //        HACK: we are using field annotations, need to skip methods otherwise we would have wrong constraints
    traverseFieldsAndMethods(type, true, false, new GatherFieldOrMethodInfoAction() {
        @Override//w  w  w  . j a  v  a 2 s.c  o m
        public void gatherInfo(AccessibleObject fOm) {
            String attributeName = getAttributeName(fOm);
            String attrPathField = attrPath + "." + attributeName;
            Class attributeType = getAttributeType(fOm);
            boolean onDeleteCascade = fOm.isAnnotationPresent(OnDelete.class)
                    ? fOm.getAnnotation(OnDelete.class).action() == OnDeleteAction.CASCADE
                    : false;

            if (fOm.isAnnotationPresent(Embedded.class)) {
                addAttributeOverrides(attr2colName, attrPathField, fOm);
                gatherCascade(attr2colName, attrPathField, attributeType);
            }

            if (fOm.isAnnotationPresent(CascadeNullable.class)) {
                if (onDeleteCascade) {
                    throw new IllegalStateException(
                            "conflicting declaration of @CascadeNullable and CascadeType on relation " + fOm);
                }
                String colName = attr2colName.get(attrPathField);
                if (colName == null) {
                    if (fOm.isAnnotationPresent(JoinColumn.class)) {
                        colName = getIdentifierForReference(fOm.getAnnotation(JoinColumn.class).name());
                    }

                    if (StringUtils.isEmpty(colName)) {
                        colName = getIdentifierForReference(attributeName);
                    }
                    columnNamesIsCascadeNullable.add(colName);
                }
            }

            if (fOm.isAnnotationPresent(NoForeignKeyConstraint.class)) {
                String colName = attr2colName.get(attrPathField);
                if (colName == null) {
                    if (fOm.isAnnotationPresent(JoinColumn.class)) {
                        colName = getIdentifierForReference(fOm.getAnnotation(JoinColumn.class).name());
                    }

                    if (StringUtils.isEmpty(colName)) {
                        colName = getIdentifierForReference(attributeName);
                    }
                    columnNamesIsNoFK.add(colName);
                }
            }

            Set<String> fkCols = new HashSet<String>();
            String fkTableName = null;
            if (fOm.isAnnotationPresent(JoinTable.class)) {
                JoinTable joinTable = fOm.getAnnotation(JoinTable.class);
                fkCols.add(getIdentifierForReference(joinTable.joinColumns()[0].name()));
                if (joinTable.inverseJoinColumns().length > 0) {
                    fkCols.add(getIdentifierForReference(joinTable.inverseJoinColumns()[0].name()));
                }
                fkTableName = joinTable.name();
            } else if (fOm.isAnnotationPresent(OneToMany.class)) {
                JoinColumn joinColumn = getJoinColumn(fOm);
                if (joinColumn != null) {
                    Class<?> targetEntityClass = MappingUtils.determineTargetEntity(fOm,
                            fOm.getAnnotation(OneToMany.class).targetEntity());
                    fkTableName = getTableAnnotation(targetEntityClass).name();
                    fkCols.add(getIdentifierForReference(joinColumn.name()));
                }
            } else if (fOm.isAnnotationPresent(ManyToOne.class) || fOm.isAnnotationPresent(OneToOne.class)) {
                JoinColumn joinColumn = getJoinColumn(fOm);
                if (joinColumn != null) {
                    fkTableName = tableName;
                    fkCols.add(getIdentifierForReference(joinColumn.name()));
                }
            }

            if (fkTableName != null && (onDeleteCascade || fOm.isAnnotationPresent(CascadeDelete.class))) {
                TableConfig fkConfig = ddlEnhancer
                        .getConfigForTableName(getIdentifierForReference(fkTableName));
                if (fkConfig != null) {
                    fkConfig.columnNamesIsCascadeDelete.addAll(fkCols);
                }
            }
        }
    });
}

From source file:corner.demo.model.many2many2.A.java

License:Apache License

/**
 * @return Returns the abs.//ww w  .  ja  v a 2  s  . c  om
 */
@OneToMany(mappedBy = "a")
@Column(length = 32, columnDefinition = "char(32)")
@OnDelete(action = OnDeleteAction.CASCADE)
public Set<AB> getAbs() {
    return abs;
}

From source file:corner.demo.model.many2many2.B.java

License:Apache License

/**
 * @return Returns the abs.//from w  w  w .  j  a  v  a2s. com
 */
@OneToMany(mappedBy = "b")
@Column(length = 32, columnDefinition = "char(32)")
@OnDelete(action = OnDeleteAction.CASCADE)
public Set<AB> getAbs() {
    return abs;
}

From source file:corner.demo.model.mulitupload.TestOne.java

License:Apache License

/**
 * @return Returns the bs.//from   w  ww.  j a v a2s.  c  o  m
 */
@OneToMany(mappedBy = "testOne")
@OnDelete(action = OnDeleteAction.CASCADE)
public List<TestMany> getTms() {
    return tms;
}

From source file:de.metanome.backend.results_db.Algorithm.java

License:Apache License

@XmlTransient
@JsonIgnore/*from w ww .  ja v a  2 s . c o  m*/
@OneToMany(fetch = FetchType.EAGER, mappedBy = "algorithm", cascade = CascadeType.ALL)
@OnDelete(action = OnDeleteAction.CASCADE)
public List<Execution> getExecutions() {
    return executions;
}

From source file:de.metanome.backend.results_db.Execution.java

License:Apache License

@OneToOne(cascade = CascadeType.ALL)
@OnDelete(action = OnDeleteAction.CASCADE)
public ExecutionSetting getExecutionSetting() {
    return executionSetting;
}

From source file:de.metanome.backend.results_db.Execution.java

License:Apache License

@OneToMany(fetch = FetchType.EAGER, mappedBy = "execution", cascade = CascadeType.ALL)
@OnDelete(action = OnDeleteAction.CASCADE)
public Set<Result> getResults() {
    return results;
}

From source file:de.tuclausthal.submissioninterface.persistence.datamodel.Lecture.java

License:Open Source License

/**
 * @return the participants//ww  w  .  ja v a2  s  .  c om
 */
@OneToMany(mappedBy = "lecture")
@OnDelete(action = OnDeleteAction.CASCADE)
// TODO: HACK!
@OrderBy(clause = "user3_.lastname,user3_.firstname")
public Set<Participation> getParticipants() {
    return participants;
}

From source file:de.tuclausthal.submissioninterface.persistence.datamodel.Lecture.java

License:Open Source License

/**
 * @return the taskGroups// w  w w.ja v a 2  s.  c  o m
 */
@OneToMany(mappedBy = "lecture", fetch = FetchType.LAZY)
@OnDelete(action = OnDeleteAction.CASCADE)
@OrderBy(clause = "taskgroupid asc")
public List<TaskGroup> getTaskGroups() {
    return taskGroups;
}

From source file:de.tuclausthal.submissioninterface.persistence.datamodel.Lecture.java

License:Open Source License

/**
 * @return the groups/*w  w  w  .j a  v  a  2s  .c  o m*/
 */
@OneToMany(mappedBy = "lecture", fetch = FetchType.LAZY)
@OnDelete(action = OnDeleteAction.CASCADE)
@OrderBy(clause = "name asc")
public Set<Group> getGroups() {
    return groups;
}