spring-data-jpa 无法创建唯一键约束条件-请确保使用正确的列名,列名取决于所使用的命名策略

9fkzdhlc  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(178)

完整的错误消息如下:

Unable to create unique key constraint (aircraft_series_id, service_enum) on table aircraft_service: database column 'service_enum' not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types)

我的实体被指定为:

@Entity
@Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "aircraft_series_id", "service_enum" }) })
@Getter
@Setter
@NoArgsConstructor
@ToString
public class AircraftService {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @NotNull
  private Integer minimumQuantity;

  @NotNull
  private Integer maximumQuantity;

  @NotNull
  private Integer defaultQuantity;

  @NotNull
  @ManyToOne(optional = false)
  @JsonIgnore
  private AircraftSeries aircraftSeries;

  @NotNull
  @Enumerated(EnumType.STRING)
  private ServiceEnum serviceEnum;

}

如果我注解掉@Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "aircraft_series_id", "service_enum" }) })注解,则会创建列,并且在SQL客户端下打开表时可以看到字段名。

service_enum
aircraft_series_id

现在,我正在对H2数据库运行应用程序。

qlvxas9a

qlvxas9a1#

我可以让应用程序在运行时不抛出异常,如果类正在吹嘘列注解,如下所示:

@Column(name = "service_enum")

@ManyToOne(optional = false)
@JoinColumn(name = "service_profile_id")

我不明白为什么会这样,因为在默认情况下,当应用程序本身赋予列名属性时,列名完全相同。

相关问题