0

A few days ago I was forced to use the following construction for my class declaration:

@Table(name="UserPattern",  uniqueConstraints={
   @UniqueConstraint(columnNames={"user_id", "patern_id"})
})

I was very surprised by this syntax.

Usually I thought that if I should to pass array to annotation O I should write the following:

declared_inside_annotation_name={value1,value2...}

but in this case it looks like the following:

uniqueConstraints={
                    @UniqueConstraint(columnNames={"user_id", "patern_id"})
                  }

@Table annotation declaration:

@Target(TYPE)
@Retention(RUNTIME)
public @interface Table {

    String name() default "";


    String catalog() default "";


    String schema() default "";


    UniqueConstraint[] uniqueConstraints() default { };


    Index[] indexes() default {};
}

please clarify this syntax.

3
  • Could you point a difference between your expected notation of declared_inside_annotation_name={value1,value2...} and actual uniqueConstraints={single_value}? Commented Jul 15, 2014 at 9:38
  • instead of value1,value2 I see @UniqueConstraint(columnNames={value1,value2}) Commented Jul 15, 2014 at 10:35
  • 1
    You confuse the @Table.uniqueConstraints, which is an array of @UniqueConstraints and contains a single item, with the value @UniqueConstraint.columnNames, which belongs to another object and is an array of strings and contains two items. Commented Jul 15, 2014 at 10:46

1 Answer 1

1

There is actually not a conflict between your expected syntax from your declared_inside_annotation_name example and the syntax from the @Table annotation. The type of elements for an array property of an annotation does not necessarily have to be a string (which might be what you have expected). It may actually be another annotation.

This is the case with the uniqueConstraints property of the @Table annotation. If you check the declaration of the UniqueConstraint class, you see that it is an annotation itself. When writing it down, you use the usual @AnnotationTypeName notation.

Sign up to request clarification or add additional context in comments.

1 Comment

Thus @UniqueConstraint(columnNames={"user_id", "patern_id"}) one array element ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.