Java @Id without @column(nullable=false)

Viewed 1877

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 Answers

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.

Related