Adding keys and AUTO_INCREMENT

exercise No. 3

Q:

Modify the current Airline class:

  1. Make the generated SQL representation using the Mysql AUTO_INCREMENT feature.

  2. Define both an airline's name and icaoCode to be represented by non - nullable unique key values. Make sure both unique key constraints receive proper self explanatory names rather then synthetic ones like e.g. UNIQUE KEY `UK_s759kv0st7r42c85xqjjiusde` (`icaoCode`).

The resulting SQL - DDL shall look like:

mysql> show create table Airline;
CREATE TABLE `Airline` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `icaoCode` varchar(255) NOT NULL,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniqueAirlineName` (`name`),
  UNIQUE KEY `uniqueIcaoCode` (`icaoCode`)
)

Tip

Read the documentation regarding:

A:

We do need the following changes with respect to :

@Entity
@Table(uniqueConstraints= {
      @UniqueConstraint(columnNames={"name"}, name="uniqueAirlineName") 
     ,@UniqueConstraint(columnNames={"icaoCode"}, name="uniqueIcaoCode") 
      })
public class Airline {
   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY) 
   Long id;
   
   @Column(nullable=false) 
   String name;

   @Column(nullable=false) 
   String icaoCode;

   ...
}

Both properties name and icaoCode will become independent unique keys among with proper SQL - DDL constraint names.

The GenerationType.IDENTITY will generate the id property as an AUTO_INCREMENT attribute. For other database vendors like e.g. Oracle™ it will become an IDENTITY attribute.

Both properties name and icaoCode will become non-nullable database attributes.