private static void buildUniqueConstraints(AnnotationDescriptor annotation, Element element) { List uniqueConstraintElementList = element.elements( "unique-constraint" ); UniqueConstraint[] uniqueConstraints = new UniqueConstraint[uniqueConstraintElementList.size()]; int ucIndex = 0; Iterator ucIt = uniqueConstraintElementList.listIterator(); while ( ucIt.hasNext() ) { Element subelement = (Element) ucIt.next(); List<Element> columnNamesElements = subelement.elements( "column-name" ); String[] columnNames = new String[columnNamesElements.size()]; int columnNameIndex = 0; Iterator it = columnNamesElements.listIterator(); while ( it.hasNext() ) { Element columnNameElt = (Element) it.next(); columnNames[columnNameIndex++] = columnNameElt.getTextTrim(); } AnnotationDescriptor ucAnn = new AnnotationDescriptor( UniqueConstraint.class ); copyStringAttribute( ucAnn, subelement, "name", false ); ucAnn.setValue( "columnNames", columnNames ); uniqueConstraints[ucIndex++] = AnnotationFactory.create( ucAnn ); } annotation.setValue( "uniqueConstraints", uniqueConstraints ); }
/** * Build a list of {@link org.hibernate.cfg.UniqueConstraintHolder} instances given a list of * {@link UniqueConstraint} annotations. * * @param annotations The {@link UniqueConstraint} annotations. * * @return The built {@link org.hibernate.cfg.UniqueConstraintHolder} instances. */ public static List<UniqueConstraintHolder> buildUniqueConstraintHolders(UniqueConstraint[] annotations) { List<UniqueConstraintHolder> result; if ( annotations == null || annotations.length == 0 ) { result = java.util.Collections.emptyList(); } else { result = new ArrayList<UniqueConstraintHolder>( CollectionHelper.determineProperSizing( annotations.length ) ); for ( UniqueConstraint uc : annotations ) { result.add( new UniqueConstraintHolder() .setName( uc.name() ) .setColumns( uc.columnNames() ) ); } } return result; }
protected <T> void register(OObjectDatabaseTx db, OSchema schema, Class<T> aClass) { if (schema.getClass(aClass.getSimpleName()) == null) { db.getEntityManager().registerEntityClasses(aClass, true); OClass cls = db.getMetadata().getSchema().getClass(aClass); String indexName = aClass.getName() + ".unq"; Table t = aClass.getAnnotation(Table.class); if (t != null) { Set<String> fields = new HashSet<>(); for (UniqueConstraint uc : t.uniqueConstraints()) { fields.addAll(Lists.newArrayList(uc.columnNames())); } if (fields.size() > 0) { LOG.info("Registering unique constraint for fields: " + fields); for (String field : fields) cls.createIndex(indexName + "." + field, OClass.INDEX_TYPE.UNIQUE_HASH_INDEX, field); } } } else { db.getEntityManager().registerEntityClasses(aClass, true); } }
private void instpectUniqueConstraint(final UniqueConstraint constraint) { UniquePropertyQuality currentQuality = UniquePropertyQuality.onlyRequiredPrimitives; final List<SingularProperty<E, ?>> uniques = new ArrayList<>(); final String[] columnNames = constraint.columnNames(); for (final String columnName : columnNames) { for (final Property<E, ?> property : this.properties.values()) { if (property instanceof SingularProperty) { final SingularProperty<E, ?> singularProperty = (SingularProperty<E, ?>) property; if (columnName.equals(singularProperty.getColumn().getName())) { final UniquePropertyQuality quality = UniquePropertyQuality.getMatchingQuality(property); if (quality != null) { if (quality.ordinal() > currentQuality.ordinal()) { currentQuality = quality; } uniques.add(singularProperty); } } } } } if (uniques.size() == columnNames.length && isBetterUniquePropertyQuality(currentQuality)) { this.uniqueProperties = uniques; this.uniquePropertiesQuality = currentQuality; } }
/** * @deprecated Use {@link #buildUniqueConstraintHolders} instead */ @Deprecated @SuppressWarnings({ "JavaDoc" }) public static List<String[]> buildUniqueConstraints(UniqueConstraint[] constraintsArray) { List<String[]> result = new ArrayList<String[]>(); if ( constraintsArray.length != 0 ) { for (UniqueConstraint uc : constraintsArray) { result.add( uc.columnNames() ); } } return result; }
private ArrayList<String[]> _getFieldsFromUniqueConstraint(Object value){ ArrayList<String[]> result = new ArrayList<>(); if(value.getClass().isAnnotationPresent(Table.class)){ Table ta = value.getClass().getAnnotation(Table.class); for(UniqueConstraint uc : ta.uniqueConstraints()){ result.add(uc.columnNames()); } } return result; }
@Override @ElementCollection(fetch = FetchType.LAZY) @CollectionTable(name = "MCRCategoryLabels", joinColumns = @JoinColumn(name = "category"), uniqueConstraints = { @UniqueConstraint(columnNames = { "category", "lang" }) }) public Set<MCRLabel> getLabels() { return super.getLabels(); }
private static String getUniqueColumns(Class<?> clazz, String name) { return Optional.of(clazz) .map(c -> c.getAnnotation(Table.class)) .map(Table::uniqueConstraints) .map(Stream::of) .flatMap(s -> s .filter(uc -> uc.name().equals(name)) .findAny() .map(UniqueConstraint::columnNames)) .map(Stream::of) .map(s -> s.collect(Collectors.joining(", "))) .get(); }
private List<String> validateCompositeUniqueConstraints(Identifiable<?> entity) { Class<?> entityClass = getClassWithoutInitializingProxy(entity); Table table = findAnnotation(entityClass, Table.class); if (table == null) { return emptyList(); } List<String> errors = newArrayList(); for (UniqueConstraint uniqueConstraint : table.uniqueConstraints()) { if (!checkCompositeUniqueConstraint(entity, entityClass, uniqueConstraint)) { errors.add(compositeUniqueConstraintErrorCode(entity, uniqueConstraint)); } } return errors; }
private Map<String, Object> getPropertyConstraints(Object entity, Class<?> entityClass, UniqueConstraint u, String prefix) { Map<String, Object> values = newHashMap(); for (String column : u.columnNames()) { Method method = columnNameToMethod(entityClass, column); if (method != null) { values.put(prefix + methodToProperty(method), invokeMethod(method, entity)); } else { Field field = columnNameToField(entityClass, column); if (field != null) { values.put(prefix + field.getName(), getValueFromField(field, entity)); } } } return values; }
private void buildUniqueProperties(final UniqueConstraint[] uniqueConstraints) { for (final UniqueConstraint constraint : uniqueConstraints) { if (constraint.columnNames().length <= this.context.getMaxUniqueProperties()) { instpectUniqueConstraint(constraint); } } }
@ElementCollection @CollectionTable(name="judgment_court_reporter", uniqueConstraints={@UniqueConstraint(name="judgment_court_reporter_unique", columnNames={"fk_judgment", "court_reporter"})}) @Column(name="court_reporter") @OrderBy("court_reporter") private List<String> getCourtReporters_() { return courtReporters; }
@ElementCollection @CollectionTable(name="judgment_legal_bases", uniqueConstraints={@UniqueConstraint(name="judgment_legal_base_unique", columnNames={"fk_judgment", "legal_base"})}) @Column(name="legal_base") @OrderBy("legal_base") private List<String> getLegalBases_() { return legalBases; }
@ElementCollection @CollectionTable(name="judgment_lower_court_judgments", uniqueConstraints={@UniqueConstraint(name="judgment_lower_court_judgment_unique", columnNames={"fk_judgment", "lower_court_judgment"})}) @Column(name="lower_court_judgment") @OrderBy("lower_court_judgment") private List<String> getLowerCourtJudgments_() { return lowerCourtJudgments; }
@ElementCollection @CollectionTable(name="ct_judgment_opinion_author", joinColumns = {@JoinColumn(name = "fk_ct_judgment_opinion", nullable = false)}, uniqueConstraints={@UniqueConstraint(name="ct_judgment_opinion_author_unique", columnNames={"fk_ct_judgment_opinion", "author"})}) @Column(name="author", nullable=false) private List<String> getAuthors_() { return authors; }
@OrderBy("id DESC") @ManyToMany(cascade = CascadeType.ALL) @JoinTable(name = "user_has_role", // joinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id", nullable = false, updatable = false)}, inverseJoinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false, updatable = false)}, uniqueConstraints = {@UniqueConstraint(columnNames = {"user_id", "role_id"})}) public List<UserEntity> getMembers() { return members; }
public void setUniqueConstraints(UniqueConstraint[] uniqueConstraints) { this.uniqueConstraints = TableBinder.buildUniqueConstraintHolders( uniqueConstraints ); }
private String compositeUniqueConstraintErrorCode(Identifiable<?> entity, UniqueConstraint uniqueConstraint) { return getEntityName(entity).toLowerCase() + "_" + (uniqueConstraint.name() == null ? "composite_unique_constraint_error" : uniqueConstraint.name().toLowerCase()); }
private boolean checkCompositeUniqueConstraint(Identifiable<?> entity, Class<?> entityClass, UniqueConstraint u) { Map<String, Object> values = newHashMap(); values.putAll(getPropertyConstraints(entity, entityClass, u, "")); return !existsInDatabaseOnAllObjects(entity, values); }
public UniqueConstraintDef(UniqueConstraint unique) { this.name=unique.name(); this.columnNames=unique.columnNames(); }
/** * Gets the value of the validation property. * * <p> * This accessor method returns a reference to the live list, not a * snapshot. Therefore any modification you make to the returned list will * be present inside the JAXB object. This is why there is not a * <CODE>set</CODE> method for the validation property. * * <p> * For example, to add a new item, do as follows: * <pre> * getValidation().add(newItem); * </pre> * * * <p> * Objects of the following type(s) are allowed in the list * {@link Validation } * * */ @ManyToMany(targetEntity = Validation.class, cascade = { CascadeType.ALL }) @JoinTable(name = "VALIDATION_REPORT_VALIDATION", joinColumns = { @JoinColumn(name = "validationreport", referencedColumnName = "HJID") }, inverseJoinColumns = { @JoinColumn(name = "validation", referencedColumnName = "HJID") }, uniqueConstraints = @UniqueConstraint(columnNames = {"validationreport", "validation"})) @OrderColumn(name = "VALIDATION_HJORDER") public Set<Validation> getValidation() { if (validation == null) { validation = new HashSet<>(); } return this.validation; }