Few days ago I finished project migration from Hibernate 5 + Hibernate Search 5 to Hibernate 6 + Hibernate Search 6.
Underlying database is MySQL and there are 19 tables that stores entity data.
With Hibernate 5 I had only one sequence file hibernate_sequence
.
To see what Hibernate 6 wants, I did first app run without any table and with option spring.jpa.hibernate.ddl-auto
set to create
. Then I notieced each table got its _seq
table.
After some analysis the idea was to upgrade existing production database by dropping hibernate_sequence
and manually creating sequence for each entity table.
Here is the SQL for single entity/table, one of 19 I have:
DROP TABLE hibernate_sequence;
CREATE TABLE article_seq ( next_val BIGINT(20) );
INSERT INTO article_seq ( next_val ) VALUES ( 1 );
UPDATE article_seq SET next_val = (
SELECT MAX(id) + 1
FROM article
);
Right after database upgrade the status is:
mysql> select max(id) from search_history_item;
+---------+
| max(id) |
+---------+
| 12131 |
+---------+
1 row in set (0.00 sec)
mysql> select * from search_history_item_seq;
+----------+
| next_val |
+----------+
| 12132 |
+----------+
1 row in set (0.00 sec)
Then I start application and reads are working as expected - pages are rendered, pagination works as expected.
But after first write attempt I get an exception:
SQL Error: 1062, SQLState: 23000
Duplicate entry â12084â for key âsearch_history_item.PRIMARYâ
DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [search_history_item.PRIMARY]
DataIntegrityViolationException: could not execute statement;
New status is the same as old status - last ID unchanged and next_val unchanged:
mysql> select max(id) from search_history_item;
+---------+
| max(id) |
+---------+
| 12131 |
+---------+
1 row in set (0.00 sec)
mysql> select * from search_history_item_seq;
+----------+
| next_val |
+----------+
| 12132 |
+----------+
1 row in set (0.00 sec)
After this second attempt to write I get almost the same exception with only difference in ID
mentioned - now this problematic ID
is 12085
(previously it was 12084
).
Java part:
class SearchHistoryItem {
@Id @GeneratedValue( strategy = GenerationType.SEQUENCE )
private Integer id;
// ...
}
What am I doing wrong?