1

I've started working on a legacy oracle database and using Spring boot Jpa trying to insert a new row in a table (CHANNELMGR_REQUEST) without Identity:

enter image description here

This table has a Numeric column (CM_ISN) and logically could be an Identity candidate, but I could not touch the database for some reason.

I found an existing sequence (CHANNELMGR_SEQ) that is used for generating value for that CM_ISN column too.

enter image description here

So I decided to use that sequence and added some annotation in my equivalent POJO as follow and mapped to the sequence to that CM_ISN column. But not touching the database :

enter image description here

My repository is like this :

enter image description here

While inserting the row, sucessfully invoke the sequence but get exception as bellow :

enter image description here

My questions :

  1. Is it wrong to modify an entity and add @Id to that which is not in in equivalent table?
  2. What's wrong with my code that i get error?

PS: I'm sorry for putting images instead of actual source codes, The reason is because development machine has no access to the internet.

Mostafa
  • 3,002
  • 10
  • 52
  • 79

1 Answers1

2
  1. It's wrong if it is not the primary key
  2. Use BigDecimal instead of Number
Edgar Domingues
  • 930
  • 1
  • 8
  • 17
  • BigDecimal solved the problem. Why the first question is wrong? what do you suggest instead? – Mostafa Jul 16 '21 at 07:26
  • 1
    By using @Id, you rely on the column to be unique, which is not enforced by the database even if it is logically unique and non-null. That might lead to very unexpected results when data gets inserted that doesn't fit the requirements of your annotation. – Kirinya Jul 16 '21 at 09:17