When externally managing the database schema (e.g. with Liquibase), in addition to specifying the Liquibase changesets, you have to help Hibernate understand your data structure using JPA annotations.
While some annotations concern higher level abstractions of the underlying data like @Embedded
or @OneToMany
, other lower level annotations, such as @Column(length = 255, nullable = false)
, seem only to represent what is already defined in the underlying database schema (varchar(255) not null
). Therefore it feels redundant to specify both, and it raises several questions for which I couldn't find a clear answer in the docs:
[main question] Which @Column
settings (besides name
, insertable
and updatable
) are only used for DDL creation and could therefore be safely omitted if the database schema is managed externally?
My guess would be: columnDefinition
, length
, precision
and scale
, but I'm unsure if Hibernate makes some other internal use of those. What about nullable
and unique
, does Hibernate take these into account for example to optimize queries, or could those be left out as well?
As a test, I tried to write null to a non-nullable database column, and the only difference in specifying
@Column(nullable = false)
was that the exception would originate from theEntityManager
instead of the database driver, which wouldn't matter much to the application (edit: assuming it is an exception case).
[side question 1] Does Hibernate assume uniqueness and non-nullability on single-column primary keys so the @Column(unique = true, nullable = false)
settings could be omitted on properties that also have @Id
?
[side question 2] Since Hibernate is capable of inspecting the database schema (e.g. when using hbm2ddl.auto=validate
), can it be configured to extract the type and constraint information it needs from the underlying schema so that settings in @Column
could be omitted?