tgr
tgr

Reputation: 3608

Hibernate will not update table with nullable OrderColumn

I want to update an existing model with an @OrderColumn. Because there already exists data in the table, I'd like to make it nullable, which is the default value.

@ManyToMany(fetch = FetchType.EAGER)
@OrderColumn(name = "widget_order", nullable = true)
private List<Widget> widgets = new ArrayList<>();

The update however fails with the message:

org.hibernate.tool.schema.spi.SchemaManagementException:
  Unable to execute schema management to JDBC target 
  [alter table Dashboard_Widget add widget_order number(10,0) not null]

Why does hibernate still try to make the order column not nullable and what do I need to do to fix it?

Upvotes: 1

Views: 499

Answers (1)

tgr
tgr

Reputation: 3608

I discovered the underlying issue by creating a new database and comparing it to the current one.

What I didn't know was, that the data structure was a Set before. The resulting composed primary key consisted of the ids of both objects. With the change to a List, the uniqueness was no longer guaranteed, even though it practically is. Therefore the new composed primary key no longer contains the widget id, but the order value.

This ultimately yields the NOT NULL constraint.

Upvotes: 1

Related Questions