Java Code Examples for javax.persistence.UniqueConstraint

The following examples show how to use javax.persistence.UniqueConstraint. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
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 );
}
 
Example 2
Source Project: lams   Source File: TableBinder.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source Project: o2oa   Source File: CheckCore.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
public static void checkTableNameUniqueConstraintName(List<Class<?>> classes) throws Exception {
	for (Class<?> cls : classes) {
		Table table = cls.getAnnotation(Table.class);
		String name = Objects.toString(FieldUtils.readStaticField(cls, "TABLE", true));
		if (!StringUtils.equals(table.name(), name)) {
			System.out.println("table name not match:" + cls);
		}
		for (UniqueConstraint u : table.uniqueConstraints()) {
			if (!StringUtils.startsWith(u.name(), table.name())) {
				System.err.println(
						String.format("checkTableNameUniqueConstraintName error: class: %s.", cls.getName()));
			}
		}
	}
}
 
Example 4
Source Project: lams   Source File: TableBinder.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @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;
}
 
Example 5
Source Project: mycore   Source File: MCRCategoryImpl.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "MCRCategoryLabels",
    joinColumns = @JoinColumn(name = "category"),
    uniqueConstraints = {
        @UniqueConstraint(columnNames = { "category", "lang" }) })
public Set<MCRLabel> getLabels() {
    return super.getLabels();
}
 
Example 6
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();
}
 
Example 7
Source Project: lams   Source File: TableBinder.java    License: GNU General Public License v2.0 4 votes vote down vote up
public void setUniqueConstraints(UniqueConstraint[] uniqueConstraints) {
	this.uniqueConstraints = TableBinder.buildUniqueConstraintHolders( uniqueConstraints );
}