0

I would like to ignore the @GeneratedValue tag while inserting data into my DB. The trick is done here but i need to support oracle (squence) and mssql (idenity) so the idgenerator is dynamically chosen by hibernate. Do you know how to tell hibernate something like : if mssql use thisone else use the other idgenerator?

PS: my mapping looks like.

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "id_generator")
Mr.H.
  • 965
  • 1
  • 10
  • 18
  • The "trick" is no trick. IDENTITY is SUPPOSED to generate ids in the datastore ... see the JPA spec. SEQUENCE is not a JPA "IDENTITY" type; it is a JPA "SEQUENCE". MSSQL "IDENTITY" ought to be handled through JPA "IDENTITY". That is why people have been saying for a long time that putting such things in annotations is a bad idea – Neil Stockton May 23 '16 at 09:43
  • sorry your answer explains why i have a problem but that does not help me. my id_generator is a SequenceGenerator, but when the DB is MSSQL jpa is ignoring my sequenceGenerator and uses its own identityGenerator. the only thing i am asking for is "how can i specify 2 id_generators and tell jpa which one to use depending on the db?" – Mr.H. May 25 '16 at 09:53
  • @NeilStockton please read the upper comment i am not sure if you have been notified because i forgot about the username – Mr.H. May 27 '16 at 10:51
  • AUTO means the JPA provider can do "what it wants" as already said. So who cares if you are putting some SequenceGenerator on there ... you put AUTO! If you put AUTO you are leaving it for someone else to decide! So don't put AUTO if you want SEQUENCE on one DB and IDENTITY on another. Have multiple orm.xml files one for MSSQL and one for the other ... and in each specify the GenerationType EXPLICITLY – Neil Stockton May 28 '16 at 17:55

0 Answers0