4

Am i right that @Id annotation add two constraints in database:

  • nullable=false
  • unique=true

? I saw a lot of examples in the Internet with syntax like

@Id
@Column(name="xxx",nullable=false)
BigInteger id

It is correct? Do i really need this nullable=false?

2 Answers2

8

Yes you are right. If you use hibernate schema generation mechanism, all @Id columns in the database will be NOT NULL and have unique index by default.

In the other hand, @Column(nullable=false) declaration is absolutely meaningless if you create the schema any other way.

0

One reason you might see the two together is for the name attribute on @Column. It's name attribute lets you explicitly choose the name of the column in the resulting table, in cases where the default name that JPA provides. I will at times use @Column solely for that purpose, just so I can give my column a certain name.

As for the nullable attribute, I agree with you. It's worthless in that case.

Nate T
  • 727
  • 5
  • 21