13

I have following entity configuration:

entity AirplaneModelSeat { 
    id Long, 
    seatNo String required 
}
relationship ManyToOne   { 
    AirplaneModelSeat{modelId(model)} to AirplaneModel 
}

This entity configuration creates such a table:

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| seat_no     | varchar(255) | NO   |     | NULL    |                |
| model_id_id | bigint(20)   | YES  | MUL | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

How can I apply unique constraint for (seat_no, model_id_id) column combination in JDL-Studio?

If this is not possible in JDL-Studioi is there any other to accomplish this?

Halil
  • 1,795
  • 1
  • 24
  • 39

3 Answers3

17

As far as I know, constraints are not part of JDL in a general manner. You can define such things as validations. But as for the Domain, the unique constraint is not that general anymore, it is a database level constraint, where it has to be applied.

For this, JHipster includes Liquibase. So you can find the changelog, defining the entity constraints in "src/main/resources/config/liquibase", and add a

<addUniqueConstraint tableName="airplane_model_seat" columnNames="seat_no, model_id_id"/>

to that changelog.

If you already started your application used h2 disk persistent databse, make a mvn clean / ./gradlew clean before starting your app again.

David Steiman
  • 3,055
  • 2
  • 16
  • 22
4

In addition to the Liquibase configuration in David's answer I suggest you also add the relevant JPA annotations to your Entity. Here is an example how:

@Table(name = "table_name",
    uniqueConstraints = {@UniqueConstraint(columnNames = {"field_1", "field_2"})})

Note that since in this case @UniqueConstraint is applied at Entity (i.e. Table) level, you can combine multiple fields into a composite unique key.

Oerd
  • 2,256
  • 1
  • 21
  • 35
0

In addition to Oerd's answer ^_^ In addition to the Liquibase configuration in David's answer I suggest you also add the relevant JPA annotations to your Entity.

If only To ensure a field value is unique you can write

@Column(unique=true)
String username;

The @UniqueConstraint annotation is for annotating multiple unique keys at the table level.

And also for Liquibase configuration, iff only To ensure a field value is unique you can write

  <column name="username" type="varchar(255)">
     <constraints nullable="true" unique="true" />
  </column>
Maosheng Wang
  • 972
  • 6
  • 12