It's necessary to declare an Identity
column in each class while developing with hibernate. Sometimes, when we need to declare a primary key as a combination of multiple columns, we call this the composite primary key, as the primary key is composed of multiple columns. We can declare a column with the primary key constraint and also generate a composite primary key using hibernate.
Let's start with a primary key declaration:
To declare a column as a primary key column, we use the
@Id
annotation, as follows:@Id private long id;
When the preceding code is executed, hibernate creates a column with the name
id
and also adds the primary key index to it. In this case,@Column
is not required unless you want a custom column name.To declare it as a composite primary key, we will consider creating a composite primary key using the employee's first name and phone. Therefore, the
firstName
column will be duplicated...