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

后端 未结 6 1106
眼角桃花
眼角桃花 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 02:57

    This works with eclipselink. It will create a seperate table for the sequence, but that shouldn't pose a problem.

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id", insertable=true, updatable=true, unique=true, nullable=false)
    private Long id;
    

    GenerationType.AUTO will choose the ideal generation strategy. Since the field is specified as insertable and updateable, a TABLE generation strategy will be used. This means eclipselink will generate another table holding the current sequence value and generate the sequence itself instead of delegating it to the database. Since the column is declared insertable, if id is null when persisting, eclipselink will generate the id. Otherwise the existing id will be used.

    0 讨论(0)
  • 2020-12-29 02:58

    Database-centric solution to your problem:

    1. Create an auxiliary, nullable column in your table. It will hold your manually assigned ids:

      CREATE TABLE `test_table`
      (
        `id` bigint(20) NOT NULL AUTO_INCREMENT,
        `manual_id` bigint(20) NULL,
        `some_other_field` varchar(200) NOT NULL,
        PRIMARY KEY(id)
      );
      
    2. Map this column to a normal field in your Entity:

      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
      @Column(name="manual_id")
      private Integer manualId;
      
    3. Create a trigger that sets the table id to the manual assigned id if it is not null:

      DELIMITER //
      CREATE TRIGGER `test_table_bi` BEFORE INSERT ON `test_table`
        FOR EACH ROW 
        BEGIN
          IF NEW.`manual_id` IS NOT NULL THEN 
            SET NEW.`id` = NEW.`manual_id`;
          END IF;
      END;//
      DELIMITER;    
      
    4. Always use the manualId when you need to assign a custom id. The trigger will do the magic for you:

      testEntiy.setManualId(300);
      entityManager.persist(testEntity);
      
    5. After the database import phase, simple remove the trigger, the auxiliary column and it's mapping.

      DROP TRIGGER `test_table_bi`;
      ALTER TABLE `test_table` DROP COLUMN `manual_id`;
      

    Warning

    If you manually specify an id greater than the current AUTO_INCREMENT value, the next generated id will jump to the value of the manually assigned id plus 1, e.g.:

    INSERT INTO `test_table` (manual_id, some_other_field) VALUES (50, 'Something');
    INSERT INTO `test_table` (manual_id, some_other_field) VALUES (NULL, 'Something else');
    INSERT INTO `test_table` (manual_id, some_other_field) VALUES (90, 'Something 2');
    INSERT INTO `test_table` (manual_id, some_other_field) VALUES (NULL, 'Something else 2');
    INSERT INTO `test_table` (manual_id, some_other_field) VALUES (40, 'Something 3');
    INSERT INTO `test_table` (manual_id, some_other_field) VALUES (NULL, 'Something else 3');
    

    Will wield the results:

    +----+-----------+------------------+
    | id | manual_id | some_other_field |
    +----+-----------+------------------+
    | 50 |        50 | Something        |
    | 51 |      NULL | Something else   |
    | 90 |        90 | Something 2      |
    | 91 |      NULL | Something else 2 |
    | 40 |        40 | Something 3      |
    | 92 |      NULL | Something else 3 |
    +----+-----------+------------------+
    

    To avoid problems it is highly recommended to set the AUTO_INCREMENT column to start with a number greater than all of the existing ids in your previous database, e.g.:

    ALTER TABLE `test_table` AUTO_INCREMENT = 100000;
    
    0 讨论(0)
  • 2020-12-29 03:01

    If you use TABLE sequencing, then EclipseLink will allow you to override the value (or SEQUENCE if your database supports this, MySQL does not).

    For IDENTITY, I'm not even sure that MySQL will allow you to supply your own Id, you might want to verify this. In general I would never recommend using IDENTITY as it does not support preallocation.

    There are a few issues with allowing IDENTITY to provide the id or not. One is that two different insert SQL will need to be generated depending on the id value, as for IDENTITY the id cannot be in the insert at all. You may wish to log a bug to have IDENTITY support user provided ids.

    You should still be able to get it working with your own Sequence subclass, or possibly MySQLPlatform subclass. You would set your MySQLPlatform subclass using the "eclipselink.target-database" persistence unit property.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-29 03:13

    Using GenerationType.SEQUENCE with PostgreSQL and EclipseLink worked for me.

    1) Change

    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    

    by

    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="step_id_seq")
    @SequenceGenerator(name="step_id_seq", sequenceName="step_id_seq")
    

    Now, you can call sequence using NativeQuery:

    return ((Vector<Integer>) em.createNativeQuery("select nextval('step_id_seq')::int").getSingleResult()).get(0);
    

    and set the returned Id to your Entity before call EntityManager.persist() method.

    Hope it's not too late!

    0 讨论(0)
  • 2020-12-29 03:20

    Look for Custom Id Generator

    http://blog.anorakgirl.co.uk/2009/01/custom-hibernate-sequence-generator-for-id-field/

    maybe this could help.

    0 讨论(0)
提交回复
热议问题