Manually specify the value of a primary key in JPA @GeneratedValue column

后端 未结 6 1147
眼角桃花
眼角桃花 2020-12-29 02:42

I\'m having an Entity which has a primary key / id field like the following:

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


        
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-29 03:08

    I might be missing something obvious, but why not just define another Entity with the same @Table(name=".....") annotation, with the same fields, but make the id not generated? Then you can use that Entity for the code that copies data from the old DB to the new, and the one with the generated Id can be used for normal creates that require id generation.

    I can't tell you if it works with EclipseLink, but we're using Hibernate here and it doesn't seem to mind it.

提交回复
热议问题